1. 有人知道java生成縮略圖的方法嗎 thumbnailator有問題

自己寫了一個,看看能不能有

/**
*本類實現一個對JPG/JPEG圖像文件進行縮放處理的方法:即給定一個JPG文件,可以生成一個該JPG文件的縮影圖像文件(JPG格式)<br/>
*提供三種生成縮影圖像的方法:<br/>
*1.設置縮影文件的寬度,根據設置的寬度和源圖像文件的大小來確定新縮影文件的長度來生成縮影圖像<br/>
*2.設置縮影文件的長度,根據設置的長度和源圖像文件的大小來確定新縮影文件的寬度來生成縮影圖像<br/>
*3.設置縮影文件相對於源圖像文件的比例大小,根據源圖像文件的大小及設置的比例來確定新縮影文件的大小來生成縮影圖像<br/>
*新生成的縮影圖像可以比原圖像大,這時即是放大源圖像<br/>
*
*@author不落的太陽(SeanYang)
*@version1.0
*@sinceJDK1.8
*
*/
publicclassImageScalingTool{

//對象是否己經初始化
privatebooleanisInitFlag=false;

//定義生目標圖片的寬度和高度,給其一個就可以了
privateinttargetPicWidth=0;
privateinttargetPicHeight=0;

//定義目標圖片的相比原圖片的比例
privatedoublepicScale=0;

/**
*構造函數
*/
publicImageScalingTool(){
isInitFlag=false;
}

/**
*重置JPG圖片縮放器
*/
publicvoidresetImageScaling(){
picScale=0;
targetPicWidth=0;
targetPicHeight=0;
isInitFlag=false;
}

/**
*設置目標圖片相對於源圖片的縮放比例
*
*@paramscale
*@throwsJPEGException
*/
publicvoidsetPicScale(doublescale)throwsException{
if(scale<=0){
thrownewRuntimeException("縮放比例不能為0和負數!");
}

resetImageScaling();
picScale=scale;
isInitFlag=true;
}

/**
*設置目標圖片的寬度
*
*@paramwidth
*@throwsJPEGException
*/
publicvoidsetSmallWidth(intwidth)throwsException{
if(width<=0){
thrownewRuntimeException("縮影圖片的寬度不能為0和負數!");
}

resetImageScaling();
targetPicWidth=width;
isInitFlag=true;
}

/**
*設置目標圖片的高度
*
*@paramheight
*@throwsJPEGException
*/
publicvoidsetSmallHeight(intheight)throwsException{
if(height<=0){
thrownewRuntimeException("縮影圖片的高度不能為0和負數!");
}

resetImageScaling();
targetPicHeight=height;
isInitFlag=true;
}

/**
*開始縮放圖片
*
*@paramsrcPicFileName
*源圖片的文件名
*@paramtargetPicFileName
*生成目標圖片的文件名
*@throwsJPEGException
*/
publicvoidtransform(StringsrcPicFileName,StringtargetPicFileName)throwsException{
if(!isInitFlag){
thrownewRuntimeException("對象參數沒有初始化!");
}
if(srcPicFileName==null||targetPicFileName==null){
thrownewRuntimeException("包含文件名的路徑為空!");
}
if((!srcPicFileName.toLowerCase().endsWith("jpg"))&&(!srcPicFileName.toLowerCase().endsWith("jpeg"))){
thrownewRuntimeException("只能處理JPG/JPEG文件!");
}
if((!targetPicFileName.toLowerCase().endsWith("jpg"))&&!targetPicFileName.toLowerCase().endsWith("jpeg")){
thrownewRuntimeException("只能處理JPG/JPEG文件!");
}

//新建源圖片和生成圖片的文件對象
Filefin=newFile(srcPicFileName);
Filefout=newFile(targetPicFileName);

//通過緩沖讀入源圖片文件
BufferedImagesourceImage=null;
try{
//讀取文件生成BufferedImage
sourceImage=ImageIO.read(fin);
}catch(IOExceptionex){
thrownewRuntimeException("讀取源圖像文件出錯!");
}
//源圖片的寬度和高度
intsourceWidth=sourceImage.getWidth();
intsourceHeight=sourceImage.getHeight();

//設置目標圖片的實際寬度和高度
inttargetWidth=0;
inttargetHeight=0;
if(targetPicWidth!=0){
//根據設定的寬度求出長度
targetWidth=targetPicWidth;
targetHeight=(targetWidth*sourceHeight)/sourceWidth;
}elseif(targetPicHeight!=0){
//根據設定的長度求出寬度
targetHeight=targetPicHeight;
targetWidth=(targetHeight*sourceWidth)/sourceHeight;
}elseif(picScale!=0){
//根據設置的縮放比例設置圖像的長和寬
targetWidth=(int)(sourceWidth*picScale);
targetHeight=(int)(sourceHeight*picScale);
}else{
thrownewRuntimeException("對象參數初始化不正確!");
}

System.out.println("源圖片的解析度:"+sourceWidth+"×"+sourceHeight);
System.out.println("目標圖片的解析度:"+targetWidth+"×"+targetHeight);
//目標圖像的緩沖對象
BufferedImagetargetImage=newBufferedImage(targetWidth,targetHeight,BufferedImage.TYPE_3BYTE_BGR);

//求得目標圖片與源圖片寬度,高度的比例.
doublescaleWidth=(double)targetWidth/sourceWidth;
doublescaleHeight=(double)targetHeight/sourceHeight;

//構造圖像變換對象
AffineTransformtransform=newAffineTransform();
//設置圖像轉換的比例
transform.setToScale(scaleWidth,scaleHeight);

//構造圖像轉換操作對象
AffineTransformOpato=newAffineTransformOp(transform,null);
//實現轉換,將bSrc轉換成bTarget
ato.filter(sourceImage,targetImage);

//輸出目標圖片
try{
//將目標圖片的BufferedImage寫到文件中去,jpeg為圖片的格式
ImageIO.write(targetImage,"jpeg",fout);
}catch(IOExceptionex1){
thrownewException("寫入縮略圖像文件出錯!");
}
}
}

2. javaWeb怎麼實現根據內容生成縮略圖

packagecom.hoo.util;

importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.MalformedURLException;
importjava.net.URL;
importjavax.imageio.ImageIO;
importcom.sun.image.codec.jpeg.ImageFormatException;
importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGEncodeParam;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;

/**
*<b>function:</b>縮放圖片工具類,創建縮略圖、伸縮圖片比例
*@authorhoojo
*@createDate2012-2-3上午10:08:47
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@version1.0
*/
{

_SCALE_QUALITY=1f;
_IMAGE_FORMAT=".jpg";//圖像文件的格式
_FILE_PATH="C:/temp-";

/**
*<b>function:</b>設置圖片壓縮質量枚舉類;
*Someguidelines:0.75highquality、0.5mediumquality、0.25lowquality
*@authorhoojo
*@createDate2012-2-7上午11:31:45
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@projectJQueryMobile
*@version1.0
*/
publicenumImageQuality{
max(1.0f),high(0.75f),medium(0.5f),low(0.25f);

privateFloatquality;
publicFloatgetQuality(){
returnthis.quality;
}
ImageQuality(Floatquality){
this.quality=quality;
}
}

privatestaticImageimage;

/**
*<b>function:</b>通過目標對象的大小和標准(指定)大小計算出圖片縮小的比例
*@authorhoojo
*@createDate2012-2-6下午04:41:48
*@paramtargetWidth目標的寬度
*@paramtargetHeight目標的高度
*@paramstandardWidth標准(指定)寬度
*@paramstandardHeight標准(指定)高度
*@return最小的合適比例
*/
publicstaticdoublegetScaling(doubletargetWidth,doubletargetHeight,doublestandardWidth,doublestandardHeight){
doublewidthScaling=0d;
doubleheightScaling=0d;
if(targetWidth>standardWidth){
widthScaling=standardWidth/(targetWidth*1.00d);
}else{
widthScaling=1d;
}
if(targetHeight>standardHeight){
heightScaling=standardHeight/(targetHeight*1.00d);
}else{
heightScaling=1d;
}
returnMath.min(widthScaling,heightScaling);
}

3. java怎麼生成swf縮略圖

public static boolean scale(String imagepath,String newpath){
// 返回一個 BufferedImage,作為使用從當前已注冊 ImageReader 中自動選擇的 ImageReader 解碼所提供 File 的結果

BufferedImage image=null;
try {
image = ImageIO.read(new File(imagepath));
} catch (IOException e) {
System.out.println("讀取圖片文件出錯!"+e.getMessage());
return false;
}

// Image Itemp = image.getScaledInstance(300, 300, image.SCALE_SMOOTH);
double Ratio = 0.0;

if ((image.getHeight() > 300) ||(image.getWidth() > 300)) {
if (image.getHeight() > image.getWidth())
//圖片要縮放的比例
Ratio = 300.0 / image.getHeight();
else
Ratio = 300.0 / image.getWidth();
}
// 根據仿射轉換和插值類型構造一個 AffineTransformOp。
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
// 轉換源 BufferedImage 並將結果存儲在目標 BufferedImage 中。
image = op.filter(image,null);
//image.getScaledInstance(300,300,image.SCALE_SMOOTH);

FileOutputStream out=null;
try {
out = new FileOutputStream(newpath);
ImageIO.write((BufferedImage)image,"bmp",out);
out.close();
} catch (Exception e) {
System.out.println("寫圖片文件出錯!!"+e.getMessage());
return false;
}
return true;
}

4. java JFileChooser選擇圖片時,如何可以看到圖片縮略圖,從而進行選擇

你可以看Swing Hacks中文版這本書第31向文件選擇器添加圖像預覽功能

5. Java中怎麼讀取縮略圖像Window中一樣,要高效,防止內存溢出! 如何分頁顯示!

java內存溢出
核心提示:原因有很多種,比如: 1.數據量過於龐大;死循環 ;靜態變數和靜態方法過多;遞歸;無法確定是否被引用的對象; 2.虛擬機不回收內存(內存泄漏); 說白了就是程序運行要用到的內存大於虛擬機能提供的最大內存就發生內存溢出了。 內存溢出的問題要看業務和系
原因有很多種,比如:
1.數據量過於龐大;死循環 ;靜態變數和靜態方法過多;遞歸;無法確定是否被引用的對象;
2.虛擬機不回收內存(內存泄漏);
說白了就是程序運行要用到的內存大於虛擬機能提供的最大內存就發生內存溢出了。 內存溢出的問題要看業務和系統大小而定,對於某些系統可能內存溢出不常見,但某些系統還是很常見的解決的方法,
一個是優化程序代碼,如果業務龐大,邏輯復雜,盡量減少全局變數的引用,讓程序使用完變數的時候釋放該引用能夠讓垃圾回收器回收,釋放資源。
二就是物理解決,增大物理內存,然後通過:-Xms256m -Xmx256m -XX:MaxNewSize=256m -XX:MaxPermSize=256m的修改

6. 求一個比較好的生成jpg圖片縮略圖的java代碼

曾經見過一個簡單的代碼你看看

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

public class JpgTest {

public void JpgTset() throws Exception{
File _file = new File("d:/5.jpg");
Image src = javax.imageio.ImageIO.read(_file);
int wideth=src.getWidth(null);
int height=src.getHeight(null);
BufferedImage tag = new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,wideth/2,height/2,null);
FileOutputStream out=new FileOutputStream("newfile.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //近JPEG編碼
//System.out.print(width+"*"+height);
out.close();
}
}

7. java後台得到圖片流,直接通過圖片流得到60*60縮略圖的流且圖片比例不變。

這個不好弄吧,如果是個100*10的圖片上傳上來,你也壓縮到60*60,比例肯定變了啊,不然就按比例縮小也可以,但是不能保證是60*60了,我有處理圖片大小的代碼,只能是按比例縮小,或者是指定寬,按原圖比例縮小圖片,直接指定寬高也可以。

8. 如何用java為文件生成縮略圖

public static boolean scale(String imagepath,String newpath){
// 返回一個 BufferedImage,作為使用從當前已注冊 ImageReader 中自動選擇的 ImageReader 解碼所提供 File 的結果

BufferedImage image=null;
try {
image = ImageIO.read(new File(imagepath));
} catch (IOException e) {
System.out.println("讀取圖片文件出錯!"+e.getMessage());
return false;
}

// Image Itemp = image.getScaledInstance(300, 300, image.SCALE_SMOOTH);
double Ratio = 0.0;

if ((image.getHeight() > 300) ||(image.getWidth() > 300)) {
if (image.getHeight() > image.getWidth())
//圖片要縮放的比例
Ratio = 300.0 / image.getHeight();
else
Ratio = 300.0 / image.getWidth();
}
// 根據仿射轉換和插值類型構造一個 AffineTransformOp。
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
// 轉換源 BufferedImage 並將結果存儲在目標 BufferedImage 中。
image = op.filter(image,null);
//image.getScaledInstance(300,300,image.SCALE_SMOOTH);

FileOutputStream out=null;
try {
out = new FileOutputStream(newpath);
ImageIO.write((BufferedImage)image,"bmp",out);
out.close();
} catch (Exception e) {
System.out.println("寫圖片文件出錯!!"+e.getMessage());
return false;
}
return true;
}

9. Java編程:怎麼獲得一個視頻的縮略圖呢

如果本地視頻的話,可以通過Runtime類的exec方法調用ffmpeg來實現
ffmpeg是視頻轉碼,截圖的程序,我這里有