Java实现的微信图片处理工具类【裁剪,合并,等比例缩放等】

  

本文实例讲述了Java实现的微信图片处理工具类。分享给大家供大家参考,具体如下:

  

现在外面核心,图片文章比较少,看了拷贝代码,而用不了,用相应的jar包处理,很多等比例缩放,达不到想要的给予的期望:本工具类,是之前做微信打印机写的基于java自带的类,基于rgb。

        包com.zjpz.util;   进口java.awt.Color;   进口java.awt.Graphics;   java . awt . graphics2d进口;   进口java.awt.RenderingHints;   进口java.awt.geom.AffineTransform;   进口java.awt.image.BufferedImage;   进口java.awt.image.ColorModel;   进口java.awt.image.WritableRaster;   进口java.io.File;   进口java.io.IOException;   进口javax.imageio.ImageIO;   进口org.slf4j.Logger;   进口org.slf4j.LoggerFactory;/* *   *微信图片处理工具   *   * @author zhuang.y   *   */公开课PictureTool {   保护静态日志记录器=LoggerFactory.getLogger (PictureTool.class);   公共静态void main (String [] args)抛出IOException {   文件我们=新文件(“c: \ \ 1. jpg”);   BufferedImage imageFirst=ImageIO.read(我们);   int边界=0;   imageFirst=作物(imageFirst 0 10297300);   文件输出文件=新文件(“d: \ \ 2. jpg”);   ImageIO。写(imageFirst“jpg”,输出文件);//写图片   }/* *   *纵向合图的x坐标像素   */私人最终静态int y_width=645;/* *   *标准图片的y坐标像素,920年是一般照片,1099是邮票照片   */私人最终静态int y_height=920;/* *   *裁剪x坐标缩进像素   */私人最终静态int x_retract=50;/* *   *裁剪y坐标缩进像素   */私人最终静态int y_retract=50;/* *   *系统默认图片边框为20   */最后公共静态int边界=20;/* *   *横向合成图片   */公共静态孔隙xPic(字符串,字符串,字符串){   尝试{/* 1读取第一张图片*/文件我们=新文件(第一);   BufferedImage imageFirst=ImageIO.read(我们);   int宽度=imageFirst.getWidth();//图片宽度   int高度=imageFirst.getHeight();//图片高度   int [] imageArrayFirst=new int(宽*高);//从图片中读取RGB   imageArrayFirst=imageFirst。getRGB(0, 0,宽度,高度,imageArrayFirst, 0,宽度);/* 1对第二张图片做相同的处理*/文件提?新文件(第二次);   BufferedImage imageSecond=ImageIO.read(提取);   int widthTwo=imageSecond.getWidth();//图片宽度   int heightTwo=imageSecond.getHeight();//图片高度   int [] imageArraySecond=new int [widthTwo * heightTwo];   imageArraySecond=imageSecond。getRGB (0, 0, widthTwo、heightTwo imageArraySecond, 0, widthTwo);   int h=身高;   如果(高度& lt;heightTwo) {   h=heightTwo;   }//生成新图片   BufferedImage imageResult=new BufferedImage(宽+ widthTwo h, BufferedImage.TYPE_INT_RGB);   imageResult。setRGB(0, 0,宽度,高度,imageArrayFirst, 0,宽度);//设置左半部分的RGB   imageResult。setRGB(宽度,0,widthTwo、heightTwo imageArraySecond, 0, widthTwo);//设置右半部分的RGB   文件输出文件=新文件(出);   ImageIO。写(imageResult“jpg”,输出文件);//写图片   }捕捉(异常e) {   logger.error(“横向合成图片出错....”,e);   }   }/* *   *纵向合成图片   *   * @param第一   *放上面的图片路径   * @param第二   *放下面的图片路径   * @param出来   *文件输出目录   * @param边境   *图片预留边框   */公共静态布尔yPic(字符串,字符串,字符串,int边境){   布尔isOk=true;   尝试{/* 1读取第一张图片*/文件我们=新文件(第一);   BufferedImage imageFirst=ImageIO.read(我们);   int宽度=imageFirst.getWidth();//图片宽度   int高度=imageFirst.getHeight();//图片高度/* 2对第二张图片做相同的处理*/文件提?新文件(第二次);   BufferedImage imageSecond=ImageIO.read(提取);   int widthTwo=imageSecond.getWidth();//图片宽度   int heightTwo=imageSecond.getHeight();//图片高度/* 1读取第一张图片开始*/int t_height=y_height - heightTwo;//图片是横图,逆时针旋转90度再等比缩放   如果(宽度比;高度){   imageFirst=rotateImageLeft90 (imageFirst);   }//等比缩放   imageFirst=调整(imageFirst y_width t_height);//缩放后图片的大小   宽度=imageFirst.getWidth();//图片宽度   身高=imageFirst.getHeight();//图片高度//等比缩放后,图片还是太大,裁剪图片   布尔a_w a_h=false;   如果(a_w=(宽度比;y_width) | | (a_h=(高度比;t_height))) {//起始位置x, y坐标   int s_w=0, s_h=0;//裁剪x坐标时,缩进属性x_retract   如果(a_w) {   int temp=- y_width宽度;   如果(临时比;x_retract) {   temp=x_retract;   其他}{   temp=0;   }   s_w=s_w +温度;   }//裁剪y坐标时,缩进属性y_retract   如果(a_h) {   int temp=身高- t_height;   如果(临时比;y_retract) {   temp=y_retract;   其他}{   temp=0;   }   s_h=s_h +温度;   }   imageFirst=作物(imageFirst s_w、s_h y_width, t_height);   宽度=imageFirst.getWidth ();   身高=imageFirst.getHeight ();   }   int [] imageArrayFirst=new int[(宽度-边界)*高);//从图片中读取RGB   imageArrayFirst=imageFirst。getRGB(边界,0,(宽度-边界),身高,imageArrayFirst, 0,   (宽度-边界));/* 2对第二张图片做相同的处理开始*/int [] imageArraySecond=new int [widthTwo * heightTwo];   imageArraySecond=imageSecond。getRGB (0, 0, widthTwo、heightTwo imageArraySecond, 0, widthTwo);   int w=宽度;   如果(宽度& lt;widthTwo) {   w=widthTwo;   }//图片高度   int h=身高+ heightTwo;//生成新图片   BufferedImage imageResult=new BufferedImage (w h BufferedImage.TYPE_INT_RGB);//解决黑色背景,默认的TYPE_INT_RGB都是0,都是黑色的   Graphics2D g=(Graphics2D) imageResult.createGraphics ();   g.setColor (Color.WHITE);   g。fillRect (0, 0 w h);//填充整个屏幕   g.dispose ();//留边框   imageResult。setRGB(边界,0,(宽度-边境* 2),身高,imageArrayFirst, 0,(宽度-边界));//设置左半部分的RGB   imageResult。setRGB(0、身高、widthTwo、heightTwo imageArraySecond, 0, widthTwo);//设置右半部分的RGB   文件输出文件=新文件(出);   ImageIO。写(imageResult“jpg”,输出文件);//写图片   }捕捉(异常e) {   logger.error(“纵向合成图片失败....”,e);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Java实现的微信图片处理工具类【裁剪,合并,等比例缩放等】