根据图片流生成缩略图

12 篇文章 0 订阅
package com.goldgrid.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ThumbnailMaker {
	/**
	 *jz 2018-9-26
	 * 生成缩略图
	 */
	
	  /**
     * @Description: 取得图片对象
     * @param 要转化的图像的文件夹,就是存放图像的文件夹路径 
     * @date 2017年5月7日10:48:27
     */
    public BufferedImage zoomImage(BufferedImage im, int toWidth , int toHeight) {
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
    /**
     * 
     * @Description: 取得图片对象
     * @param 要转化的图像的文件夹,就是存放图像的文件夹路径 
     * @date 2017年5月7日10:48:27
     */
    public BufferedImage getImageList(String ImgList, String[] type) throws IOException{
        Map<String,Boolean> map = new HashMap<String, Boolean>();
        for(String s : type) {
            map.put(s,true);
        }
       BufferedImage imageList = null;
        File file = null;
            file = new File(ImgList);
            try{
                if(file.length() != 0 && map.get(getExtension(file.getName())) != null ){
                    imageList = javax.imageio.ImageIO.read(file);
                }
            }catch(Exception e){
                imageList = null;
            }

        return imageList;
    }

    /**
     * 把图片写到磁盘上
     * @param im
     * @param path     eg: C://home// 图片写入的文件夹地址
     * @param fileName DCM1987.jpg  写入图片的名字
     * @date 2017年5月7日10:48:27
     */

    public boolean writeToDisk(BufferedImage im, String path, String fileName) {
        File f = new File(path + fileName);
        String fileType = getExtension(fileName);
        if (fileType == null)
            return false;
        try {
            ImageIO.write(im, fileType, f);
            im.flush();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    /**
     * 
     * @Description: 生成图片
     * @param String path , BufferedImage im, String fileFullPath 
     * @date 2017年5月7日10:48:27
     */
    public boolean writeHighQuality(String path , BufferedImage im, String fileFullPath) throws IOException {
            //return true;
            FileOutputStream newimage = null;
        try {
           // 输出到文件流
            newimage = new FileOutputStream(fileFullPath+"/"+path);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
            // 压缩质量 
            jep.setQuality(1f, true);
            encoder.encode(im, jep);
           //近JPEG编码
            newimage.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * 
     * @Description: 取文件名的后缀
     * @param String fileName 格式如:cn1100000213EA_1_xnl.jpg
     * @date 2017年5月7日10:48:27
     */
    public String getExtension(String fileName) {
        try {
            return fileName.split("\\.")[fileName.split("\\.").length - 1];
        } catch (Exception e) {
            return null;
        }
    }


    public static void main(String[] args) throws IOException {
    	String a="d:\\i.jpg";
		File file=new File(a);
		FileInputStream fileInputStream=new FileInputStream(file);
		byte[] b=new byte[819200];
		fileInputStream.read(b);
		InputStream in = new ByteArrayInputStream(b); 
		BufferedImage image = ImageIO.read(in); 

		System.out.println(image);
    	
    	String outputFolder = "G:\\Eclipse\\eclipse\\EclipseOxygenDev\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp6\\wtpwebapps\\iSolutions-BDE\\uploadpicture\\";
        String fileName = "D:\\i.jpg";
        ThumbnailMaker r = new ThumbnailMaker();
        int toWidth=200;
        int toHeight=240;
        BufferedImage imageList = r.getImageList(fileName,new String[] {"jpg","png","gif"});
        r.writeHighQuality("newFile.jpg",r.zoomImage(imageList,toWidth,toHeight),outputFolder);

	}
}
                         in = new SmbFileInputStream(f);
							
							/*byte[] b = new byte[8192];
							int n = 0;
							while ((n = in.read(b)) > 0){
								out.write(b, 0, n);
							}*/
							
							/*
							 * 查看图片服务器上的图片时,将获取的图片流生成缩略图展示。
							 * jz 2018-9-27
							 */
							BufferedImage image = ImageIO.read(in);
							ThumbnailMaker r = new ThumbnailMaker();
					        int toWidth=200;
					        int toHeight=240;
					        BufferedImage bufferedImage=r.zoomImage(image,toWidth,toHeight);
					        ImageIO.write(bufferedImage,"jpg",output);
					        is = new ByteArrayInputStream(output.toByteArray());
					        byte[] c = new byte[8192];
					        int m=0;
					        while ((m = is.read(c)) > 0){
								out.write(c,0,m);
					        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值