java 图片生成缩略图后,转化成流

功能:图片生成缩略图后,转化成流

public class ImageUtils {
    /**
     * 
     * @param in1
     *            文件流
     * @param uploadFileName
     *            文件名称
     * @param wide
     *            宽
     * @param high
     *            高
     * @return
     * @throws IOException
     */
    public static InputStream thumbnailImage(InputStream in1, String fileName, int wide, int high) throws IOException {
        InputStream inThumb = null;

        String types = Arrays.toString(ImageIO.getReaderFormatNames()).replace("]", ",");
        String suffix = null;
        if (fileName.indexOf(".") > -1) {
            suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        }// 类型和图片后缀全部小写,然后判断后缀是否合法
        if (suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase() + ",") < 0) {
            return null;
        }
        Image img = ImageIO.read(in1);
        BufferedImage bi = new BufferedImage(wide, high, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.drawImage(img, 0, 0, wide, high, Color.LIGHT_GRAY, null);
        g.dispose();
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imOut;
        imOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(bi, "jpg", imOut);
        inThumb = new ByteArrayInputStream(bs.toByteArray());
        return inThumb;
    }
}

 

posted on 2015-05-11 17:17  依米艳  阅读(764)  评论(0编辑  收藏  举报

导航