㈠ java上傳圖片 生成縮略圖,如果上傳的圖片尺寸比較小就壓縮處理
//將圖按比例縮小。
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分別表示目標長和寬
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
//這里想實現在targetW,targetH范圍內實現等比縮放。如果不需要等比縮放
//則將下面的if else語句注釋即可
if(sx>sy)
{
sx = sy;
targetW = (int)(sx * source.getWidth());
}else{
sy = sx;
targetH = (int)(sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
throws Exception {
BufferedImage srcImage;
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
String imgType = "JPEG";
if (fromFileStr.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
// System.out.println(ex);
File saveFile=new File(saveToFileStr);
File fromFile=new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if(width > 0 || hight > 0)
{
srcImage = resize(srcImage, width, hight);
}
ImageIO.write(srcImage, imgType, saveFile);
}
public static void main (String argv[]) {
try{
//參數1(from),參數2(to),參數3(寬),參數4(高)
saveImageAsJpg("C:\\Documents and Settings\\xugang\\桌面\\tmr-06.jpg",
"C:\\Documents and Settings\\xugang\\桌面\\2.jpg",
120,120);
} catch(Exception e){
e.printStackTrace();
}
}
㈡ 如何利用JAVA直接生成Flash(SWF格式)文件的縮略圖謝謝了,大神幫忙啊
不知用Java怎麼直接生成,只知道用fla格式轉換成swf格式,就用Flash8保存為fla格式,再在預覽時按Ctrl+Enter鍵,就可以生成一個SWF格式的文件,希望有用
㈢ 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);
}
㈣ java後台得到圖片流,直接通過圖片流得到60*60縮略圖的流且圖片比例不變。
這個不好弄吧,如果是個100*10的圖片上傳上來,你也壓縮到60*60,比例肯定變了啊,不然就按比例縮小也可以,但是不能保證是60*60了,我有處理圖片大小的代碼,只能是按比例縮小,或者是指定寬,按原圖比例縮小圖片,直接指定寬高也可以。
㈤ 如何利用Java生成JPG縮略圖
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;
}
㈥ 有人知道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("寫入縮略圖像文件出錯!");
}
}
}
㈦ JAVA開發微信公眾號圖文素材縮略圖,怎麼設置圖文縮略圖
你好。 如果要把別人的圖片水印變成自己的,那麼首先就是用PS軟體P除掉該圖片上的水印,然後將P過的圖片加入微信公眾平台素材庫里。 方法一:點擊微信公眾平台後台的公眾號設置——功能設置裡面有個圖片水印 然後你就可以選著修改水印或者圖片不添...
㈧ java實現圖片預覽功能,可以顯示縮列圖,具有上下頁的功能求技術支持
把圖片按照規定的比例壓縮,然後保存至FTP,列表讀取縮略圖,單擊顯示原圖。
/**
*壓縮圖片方法一(高質量)
*@paramoldFile將要壓縮的圖片
*@paramwidth壓縮寬
*@paramheight壓縮高
*@paramsmallIcon壓縮圖片後,添加的擴展名(在圖片後綴名前添加)
*@paramquality壓縮質量范圍:<i>0.0-1.0</i>高質量:<i>0.75</i>中等質量:<i>0.5</i>低質量:<i>0.25</i>
*@parampercentage是否等比壓縮若true寬高比率將將自動調整
*/
publicstaticvoidcompressImage(StringoldFile,intwidth,intheight,StringsmallIcon,
floatquality,booleanpercentage){
try{
Filefile=newFile(oldFile);
//驗證文件是否存在
if(!file.exists())
thrownewFileNotFoundException("找不到原圖片!");
//獲取圖片信息
BufferedImageimage=ImageIO.read(file);
intorginalWidth=image.getWidth();
intorginalHeight=image.getHeight();
//驗證壓縮圖片信息
if(width<=0||height<=0||!Pattern.matches("^[1-9]\d*$",String.valueOf(width))
||!Pattern.matches("^[1-9]\d*$",String.valueOf(height)))
thrownewException("圖片壓縮後的高寬有誤!");
//等比壓縮
if(percentage){
doublerate1=((double)orginalWidth)/(double)width+0.1;
doublerate2=((double)orginalHeight)/(double)height+0.1;
doublerate=rate1>rate2?rate1:rate2;
width=(int)(((double)orginalWidth)/rate);
height=(int)(((double)orginalHeight)/rate);
}
//壓縮後的文件名
StringfilePrex=oldFile.substring(0,oldFile.lastIndexOf('.'));
StringnewImage=filePrex+smallIcon+oldFile.substring(filePrex.length());
//壓縮文件存放位置
FilesavedFile=newFile(newImage);
//創建一個新的文件
savedFile.createNewFile();
//創建原圖像的縮放版本
Imageimage2=image.getScaledInstance(width,height,Image.SCALE_AREA_AVERAGING);
//創建數據緩沖區圖像
BufferedImagebufImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//創建一個Graphics2D
Graphics2Dg2=bufImage.createGraphics();
//重繪圖像
g2.drawImage(image2,0,0,width,height,null);
g2.dispose();
//過濾像素矩陣
float[]kernelData={
-0.125f,-0.125f,-0.125f,
-0.125f,2,-0.125f,-0.125f,
-0.125f,-0.125f};
Kernelkernel=newKernel(3,3,kernelData);
//按核數學源圖像邊緣的像素復制為目標中相應的像素輸出像素
ConvolveOpcOp=newConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
//轉換像素
bufImage=cOp.filter(bufImage,null);
FileOutputStreamout=newFileOutputStream(savedFile);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(bufImage);
//設置壓縮質量
param.setQuality(quality,true);
encoder.encode(bufImage,param);
out.close();
System.out.println(newImage);
}catch(Exceptione){
e.printStackTrace();
System.out.println("壓縮失敗!"+e.getMessage());
}
}
㈨ 圖片怎麼樣生成縮略圖啊 java
您可以用jquery組件有現成的 會直接生成縮略圖
㈩ java 圖片縮放代碼
直接給你一個類,直接套用就好了
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class Resize {
BufferedImage bufImage;
int width;
int height;
public Resize() {
// TODO Auto-generated constructor stub
}
public Resize(String srcPath,int width,int height) {
this.width = width;
this.height = height;
try{
this.bufImage = ImageIO.read(new File(srcPath));
}catch(Exception e){
e.printStackTrace();
}
}
public static BufferedImage rize(BufferedImage srcBufImage,int width,int height){
BufferedImage bufTarget = null;
double sx = (double) width / srcBufImage.getWidth();
double sy = (double) height / srcBufImage.getHeight();
int type = srcBufImage.getType();
if(type == BufferedImage.TYPE_CUSTOM){
ColorModel cm = srcBufImage.getColorModel();
WritableRaster raster = cm.(width,
height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
}else
bufTarget = new BufferedImage(width, height, type);
Graphics2D g = bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return bufTarget;
}
}