前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2018-03-01 thumbnailator图片处理接收Byte数组

2018-03-01 thumbnailator图片处理接收Byte数组

作者头像
Albert陈凯
发布2018-09-20 16:22:23
1.8K0
发布2018-09-20 16:22:23
举报
文章被收录于专栏:Albert陈凯

thumbnailator工具以及Java原生的图片处理工具类,效率上还是原生的较好,不过thumbnailator提供的功能较全面也较简单,需要注意的是thumbnailator处理png格式的图片时,如果遇到透明背景的话,会处理成黑色的背景,处理方法就是在使用thumbnailator处理png图像之前将其背景图设置为白色,这是最简单有效的办法,各位看官如果有其他行之有效的方法,欢迎随时留言

代码语言:javascript
复制
package com.*.util;  
  
import java.awt.Color;  
import java.awt.Graphics2D;  
import java.awt.Rectangle;  
import java.awt.image.BufferedImage;  
import java.io.ByteArrayInputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Iterator;  
  
import javax.imageio.ImageIO;  
import javax.imageio.ImageReadParam;  
import javax.imageio.ImageReader;  
import javax.imageio.stream.ImageInputStream;  
  
import net.coobird.thumbnailator.Thumbnailator;  
import net.coobird.thumbnailator.Thumbnails;  
import net.coobird.thumbnailator.geometry.Positions;  
  
public class ImageUtils  
{  
  
      
    /** 
     * jpg文件格式 
     */  
    public static String JPG = "jpg";  
      
    /** 
     * png文件格式 
     */  
    public static String PNG = "png";  
      
    /** 
     * 图片裁剪工具 
     *  
     * @param in 
     *            ******************输入流 
     * @param readImageFormat 
     *            *****图片格式 
     * @param x 
     *            *******************起始点x坐标 
     * @param y 
     *            *******************起始点y坐标 
     * @param w 
     *            *******************裁剪宽度 
     * @param h 
     *            *******************裁剪高度 
     * @return is*****************输出流 
     * @author Jieve 
     * @throws IOException 
     */  
    public static InputStream cutImage(InputStream in, String readImageFormat, int x, int y, int w, int h) throws IOException  
    {  
  
        Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(readImageFormat);  
        ImageReader reader = (ImageReader) iterator.next();  
        ImageInputStream iis = ImageIO.createImageInputStream(in);  
        reader.setInput(iis, true);  
        ImageReadParam param = reader.getDefaultReadParam();  
        Rectangle rect = new Rectangle(x, y, w, h);  
        param.setSourceRegion(rect);  
        BufferedImage bi = reader.read(0, param);  
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        ImageIO.write(bi, readImageFormat, os);  
        InputStream is = new ByteArrayInputStream(os.toByteArray());  
        in.close();  
        os.close();  
        return is;  
    }  
  
    /** 
     * 根据图片对象获取对应InputStream 
     *  
     * @param image 
     * @param readImageFormat 
     * @return 
     * @throws IOException 
     */  
    public static InputStream getInputStream(BufferedImage image, String readImageFormat) throws IOException  
    {  
  
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        ImageIO.write(image, readImageFormat, os);  
        InputStream is = new ByteArrayInputStream(os.toByteArray());  
        os.close();  
        return is;  
    }  
      
    /** 
     * 生成缩略图 
     *  
     * @param in 
     * @param size 
     * @param imageFormat 
     * @return 
     * @throws IOException 
     */  
    public static InputStream createThumbnails(InputStream in, int size, String imageFormat) throws IOException  
    {  
        if(imageFormat.equalsIgnoreCase("jpg"))  
        {  
              
            return createThumbnails(in, size);  
        }  
        else if(imageFormat.equalsIgnoreCase("png"))  
        {  
        BufferedImage image = Thumbnails.of(in).scale(1f).outputFormat(JPG).asBufferedImage();  
        InputStream is = getInputStream(image, JPG);  
        return createThumbnails(is, size);  
        }  
        return null;  
    }  
      
    /** 
     * 转化为jpg格式 
     *  
     * @param in 
     * @param imageFormat 
     * @return 
     * @throws IOException 
     */  
    public static BufferedImage toJPG(BufferedImage image) throws IOException{  
          
        int width = image.getWidth();  
        int height = image.getHeight();  
        BufferedImage image_ = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        Graphics2D graphic = image_.createGraphics();  
        graphic.setColor(Color.WHITE);  
        graphic.fillRect(0,0,width,height);  
        graphic.drawRenderedImage(image, null);  
        graphic.dispose();  
        return image_;  
    }  
  
    /** 
     * 生成缩略图 
     *  
     * @param in 
     * @param width 
     * @param height 
     * @return 
     * @throws IOException 
     */  
    public static InputStream createThumbnails(InputStream in, int width, int height) throws IOException  
    {  
  
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        Thumbnailator.createThumbnail(in, os, width, height);  
        InputStream is = new ByteArrayInputStream(os.toByteArray());  
        in.close();  
        os.close();  
        return is;  
    }  
  
    /** 
     * 生成缩略图 
     *  
     * @param in 
     * @param size 
     * @return 
     * @throws IOException 
     */  
    public static InputStream createThumbnails(InputStream in, int size) throws IOException  
    {  
  
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        Thumbnailator.createThumbnail(in, os, size, size);  
        InputStream is = new ByteArrayInputStream(os.toByteArray());  
        in.close();  
        os.close();  
        return is;  
    }  
  
    /** 
     * 添加水印 
     *  
     * @param in 
     * @param width 
     * @param height 
     * @return 
     * @throws IOException 
     */  
    public static InputStream addWaterMark(InputStream in, InputStream waterMark, int width, int height) throws IOException  
    {  
  
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        Thumbnails.of(in).size(width, height).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(waterMark), 0.4f).outputQuality(0.8f).toOutputStream(os);  
        InputStream is = new ByteArrayInputStream(os.toByteArray());  
        in.close();  
        waterMark.close();  
        os.close();  
        return is;  
    }  
  
    /** 
     * 居中裁剪 
     *  
     * @param image 
     * @return 
     */  
    public static BufferedImage clipCenter(BufferedImage image)  
    {  
  
        int height = image.getHeight();  
        int width = image.getWidth();  
        int size = height >= width ? width : height;  
        int temp = 0;  
        if(height >= width)  
        {  
            temp = (height - width) / 2;  
            image = image.getSubimage(0, temp, size, size);  
        }  
        else  
        {  
            temp = (width - height) / 2;  
            image = image.getSubimage(temp, 0, size, size);  
        }  
  
        return image;  
    }  
  
    /** 
     * 裁剪图片 
     *  
     * @param image 
     * @param x 
     * @param y 
     * @param size 
     * @return 
     */  
    public static BufferedImage cutImage(BufferedImage image, int x, int y, int size)  
    {  
  
        int height = image.getHeight();  
        int width = image.getWidth();  
        if((width >= (x + size)) && (height >= (y + size)))  
        {  
            image = image.getSubimage(x, y, size, size);  
        }  
        else  
        {  
            int temp = ((height - y) >= (width - x)) ? (width - x) : (height - y);  
            image = image.getSubimage(x, y, temp, temp);  
        }  
  
        return image;  
    }  
  
    /** 
     * 检查格式是否合法 
     *  
     * @param imageType 
     * @return 
     */  
    public static boolean checkType(String imageType)  
    {  
  
        boolean flag = false;  
        if(JPG.equalsIgnoreCase(imageType) || PNG.equalsIgnoreCase(imageType))  
        {  
            flag = true;  
        }  
        return flag;  
    }  
  
    /** 
     * 压缩图片 
     * 默认输出50%质量图片 
     *  
     * @param image 
     * @return 
     * @throws IOException 
     */  
    public static InputStream compress(BufferedImage image, String readImageFormat) throws IOException  
    {  
  
        InputStream in = ImageUtils.getInputStream(image, readImageFormat);  
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        Thumbnails.of(in).size(image.getWidth(), image.getHeight()).outputQuality(0.5f).toOutputStream(os);  
        InputStream is = new ByteArrayInputStream(os.toByteArray());  
        in.close();  
        os.close();  
        return is;  
    }  
}  

http://blog.csdn.net/jieve_y/article/details/51283748

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.03.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图片处理
图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档