1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| package com.vshow.control.tool;
import java.awt.Image;
import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
public static void main(String[] args) throws Exception { File file = new File("D:"+File.separator+"nsh.png"); System.out.println(file.getPath()); ImageUtil.makeSmallImageT(file,"png","_nsh.png"); }
public static void makeSmallImage(File srcImageFile,String dstImageFileName) throws Exception{ FileOutputStream fileOutputStream = null; JPEGImageEncoder encoder = null; BufferedImage tagImage = null; Image srcImage = null; try{ srcImage = ImageIO.read(srcImageFile); int srcWidth = srcImage.getWidth(null); int srcHeight = srcImage.getHeight(null); int dstMaxSize = 120; int dstWidth = srcWidth; int dstHeight = srcHeight; float scale = 0; if(srcWidth>dstMaxSize){ dstWidth = dstMaxSize; scale = (float)srcWidth/(float)dstMaxSize; dstHeight = Math.round((float)srcHeight/scale); } srcHeight = dstHeight; if(srcHeight>dstMaxSize){ dstHeight = dstMaxSize; scale = (float)srcHeight/(float)dstMaxSize; dstWidth = Math.round((float)dstWidth/scale); } tagImage = new BufferedImage(dstWidth,dstHeight,BufferedImage.TYPE_INT_RGB); tagImage.getGraphics().drawImage(srcImage,0,0,dstWidth,dstHeight,null); fileOutputStream = new FileOutputStream(dstImageFileName); encoder = JPEGCodec.createJPEGEncoder(fileOutputStream); encoder.encode(tagImage); fileOutputStream.close(); fileOutputStream = null; }finally{ if(fileOutputStream!=null){ try{ fileOutputStream.close(); }catch(Exception e){ } fileOutputStream = null; } encoder = null; tagImage = null; srcImage = null; System.gc(); } }
public static void makeSmallImageT(File srcImageFile,String type,String tempname) {
try { FileHandle.copy3(srcImageFile.getPath(), Constant.FILES + File.separator +"_"+tempname+"."+type); } catch (Exception e) { FileHandle.copy3(Constant.FILES + File.separator +tempname, Constant.FILES + File.separator +"_"+tempname); } } }
|