Java实现的微信图片处理工具类【裁剪,合并,等比例缩放等】
程序员文章站
2024-02-26 19:32:16
本文实例讲述了java实现的微信图片处理工具类。分享给大家供大家参考,具体如下:
现在 外面核心,图片文章比较少,看了拷贝代码,而用不了,用相应jar包处理,很多等比例缩...
本文实例讲述了java实现的微信图片处理工具类。分享给大家供大家参考,具体如下:
现在 外面核心,图片文章比较少,看了拷贝代码,而用不了,用相应jar包处理,很多等比例缩放,达不到 想要的给予的期望:本工具类,是之前做微信打印机写的 基于java自带的类,基于rgb。
package com.zjpz.util; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import java.awt.image.colormodel; import java.awt.image.writableraster; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import org.slf4j.logger; import org.slf4j.loggerfactory; /** * 微信图片处理工具 * * @author zhuang.y * */ public class picturetool { protected static logger logger = loggerfactory.getlogger(picturetool.class); public static void main(string[] args) throws ioexception { file fileone = new file("c:\\1.jpg"); bufferedimage imagefirst = imageio.read(fileone); int border = 0; imagefirst =crop(imagefirst,0,10,297,300); file outfile = new file("d:\\2.jpg"); imageio.write(imagefirst, "jpg", outfile);// 写图片 } /** * 纵向合图的x坐标像素 */ private final static int y_width = 645; /** * 标准图片的y坐标像素,920,是一般照片,1099是邮票照片 */ private final static int y_height = 920; /** * 裁剪x坐标缩进像素 */ private final static int x_retract = 50; /** * 裁剪y坐标缩进像素 */ private final static int y_retract = 50; /** * 系统默认图片边框为20 */ public final static int border = 20; /** * 横向合成图片 */ public static void xpic(string first, string second, string out) { try { /* 1 读取第一张图片 */ file fileone = new file(first); bufferedimage imagefirst = imageio.read(fileone); int width = imagefirst.getwidth();// 图片宽度 int height = imagefirst.getheight();// 图片高度 int[] imagearrayfirst = new int[width * height];// 从图片中读取rgb imagearrayfirst = imagefirst.getrgb(0, 0, width, height, imagearrayfirst, 0, width); /* 1 对第二张图片做相同的处理 */ file filetwo = new file(second); bufferedimage imagesecond = imageio.read(filetwo); 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 = height; if (height < heighttwo) { h = heighttwo; } // 生成新图片 bufferedimage imageresult = new bufferedimage(width + widthtwo, h, bufferedimage.type_int_rgb); imageresult.setrgb(0, 0, width, height, imagearrayfirst, 0, width);// 设置左半部分的rgb imageresult.setrgb(width, 0, widthtwo, heighttwo, imagearraysecond, 0, widthtwo);// 设置右半部分的rgb file outfile = new file(out); imageio.write(imageresult, "jpg", outfile);// 写图片 } catch (exception e) { logger.error("横向合成图片出错....", e); } } /** * 纵向合成图片 * * @param first * 放上面的图片路径 * @param second * 放下面的图片路径 * @param out * 文件输出目录 * @param border * 图片预留边框 */ public static boolean ypic(string first, string second, string out, int border) { boolean isok = true; try { /* 1 读取第一张图片 */ file fileone = new file(first); bufferedimage imagefirst = imageio.read(fileone); int width = imagefirst.getwidth();// 图片宽度 int height = imagefirst.getheight();// 图片高度 /* 2对第二张图片做相同的处理 */ file filetwo = new file(second); bufferedimage imagesecond = imageio.read(filetwo); int widthtwo = imagesecond.getwidth();// 图片宽度 int heighttwo = imagesecond.getheight();// 图片高度 /* 1 读取第一张图片begin */ int t_height = y_height - heighttwo; // 图片是横图,逆时针旋转90度再等比缩放 if (width > height) { imagefirst = rotateimageleft90(imagefirst); } // 等比缩放 imagefirst = resize(imagefirst, y_width, t_height); // 缩放后图片的大小 width = imagefirst.getwidth();// 图片宽度 height = imagefirst.getheight();// 图片高度 // 等比缩放后,图片还是太大,裁剪图片 boolean a_w, a_h = false; if ((a_w = (width > y_width)) || (a_h = (height > t_height))) { // 起始位置x,y坐标 int s_w = 0, s_h = 0; // 裁剪x坐标时,缩进属性x_retract if (a_w) { int temp = width - y_width; if (temp > x_retract) { temp = x_retract; } else { temp = 0; } s_w = s_w + temp; } // 裁剪y坐标时,缩进属性y_retract if (a_h) { int temp = height - t_height; if (temp > y_retract) { temp = y_retract; } else { temp = 0; } s_h = s_h + temp; } imagefirst = crop(imagefirst, s_w, s_h, y_width, t_height); width = imagefirst.getwidth(); height = imagefirst.getheight(); } int[] imagearrayfirst = new int[(width - border) * height];// 从图片中读取rgb imagearrayfirst = imagefirst.getrgb(border, 0, (width - border), height, imagearrayfirst, 0, (width - border)); /* 2对第二张图片做相同的处理begin */ int[] imagearraysecond = new int[widthtwo * heighttwo]; imagearraysecond = imagesecond.getrgb(0, 0, widthtwo, heighttwo, imagearraysecond, 0, widthtwo); int w = width; if (width < widthtwo) { w = widthtwo; } // 图片高度 int h = height + 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(border, 0, (width - border * 2), height, imagearrayfirst, 0, (width - border));// 设置左半部分的rgb imageresult.setrgb(0, height, widthtwo, heighttwo, imagearraysecond, 0, widthtwo);// 设置右半部分的rgb file outfile = new file(out); imageio.write(imageresult, "jpg", outfile);// 写图片 } catch (exception e) { logger.error("纵向合成图片失败....", e); isok = false; } return isok; } /** * 全图打印,图片缩放、旋转处理 * * @param source * 待处理的图片 * @param out * 处理后文件输出目录 * @param border * 图片预留边框 */ public static boolean maigaopic(string source, string out, int border) { boolean isok = true; try { /* 1 读取第一张图片 */ file fileone = new file(source); bufferedimage imagefirst = imageio.read(fileone); int width = imagefirst.getwidth();// 图片宽度 int height = imagefirst.getheight();// 图片高度 // 图片是横图,逆时针旋转90度再等比缩放 if (width > height) { imagefirst = rotateimageleft90(imagefirst); } // 等比缩放 imagefirst = resize(imagefirst, y_width, y_height); // 缩放后图片的大小 width = imagefirst.getwidth();// 图片宽度 height = imagefirst.getheight();// 图片高度 // 等比缩放后,图片还是太大,裁剪图片 boolean a_w, a_h = false; if ((a_w = (width > y_width)) || (a_h = (height > y_height))) { // 起始位置x,y坐标 int s_w = 0, s_h = 0; // 裁剪x坐标时,缩进属性x_retract if (a_w) { int temp = width - y_width; if (temp > x_retract) { temp = x_retract; } else { temp = 0; } s_w = s_w + temp; } // 裁剪y坐标时,缩进属性y_retract if (a_h) { int temp = height - y_height; if (temp > y_retract) { temp = y_retract; } else { temp = 0; } s_h = s_h + temp; } imagefirst = crop(imagefirst, s_w, s_h, y_width, y_height); width = imagefirst.getwidth(); height = imagefirst.getheight(); } int[] imagearrayfirst = new int[(width - border) * height];// 从图片中读取rgb imagearrayfirst = imagefirst.getrgb(border, 0, (width - border), height, imagearrayfirst, 0, (width - border)); // 生成新图片 bufferedimage imageresult = new bufferedimage(width, height, bufferedimage.type_int_rgb); // 解决黑色背景,默认的type_int_rgb都是0,都是黑色的 graphics2d g = (graphics2d) imageresult.creategraphics(); g.setcolor(color.white); g.fillrect(0, 0, width, height);// 填充整个屏幕 g.dispose(); // 留边框 imageresult.setrgb(border, 0, (width - border * 2), height, imagearrayfirst, 0, (width - border));// 设置左半部分的rgb file outfile = new file(out); imageio.write(imageresult, "jpg", outfile);// 写图片 } catch (ioexception e) { logger.error("全图打印,图片缩放、旋转处理失败....", e); isok = false; } return isok; } /** * 实现图像的等比缩放 * * @param source * 待处理的图片流 * @param targetw * 宽度 * @param targeth * 高度 * @return */ public static bufferedimage resize(bufferedimage source, int targetw, int targeth) { int width = source.getwidth();// 图片宽度 int height = source.getheight();// 图片高度 return zoominimage(source, targetw, targeth); // 图片宽高都太小时,强制放大图片 /* if (width < targetw && height < targeth) { return zoominimage(source, targetw, targeth); } else if ((width < targetw && width == height) || (height < targeth && width == height)) { return zoominimage(source, targetw, targeth); } return null; */ } /** * 按比例裁剪图片 * * @param source * 待处理的图片流 * @param startx * 开始x坐标 * @param starty * 开始y坐标 * @param endx * 结束x坐标 * @param endy * 结束y坐标 * @return */ public static bufferedimage crop(bufferedimage source, int startx, int starty, int endx, int endy) { int width = source.getwidth(); int height = source.getheight(); if (startx <= -1) { startx = 0; } if (starty <= -1) { starty = 0; } if (endx <= -1) { endx = width - 1; } if (endy <= -1) { endy = height - 1; } bufferedimage result = new bufferedimage(endx, endy , source.gettype()); for (int y = starty; y < endy+starty; y++) { for (int x = startx; x < endx+startx; x++) { int rgb = source.getrgb(x, y); result.setrgb(x - startx, y - starty, rgb); } } return result; } /** * 旋转图片为指定角度 * * @param bufferedimage * 目标图像 * @param degree * 旋转角度 * @return */ public static bufferedimage rotateimage(final bufferedimage bufferedimage, final int degree) { int w = bufferedimage.getwidth(); int h = bufferedimage.getheight(); int type = bufferedimage.getcolormodel().gettransparency(); bufferedimage img; graphics2d graphics2d; (graphics2d = (img = new bufferedimage(h, w, type)).creategraphics()).setrenderinghint( renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); graphics2d.rotate(math.toradians(degree), w / 2, h / 2 + (w > h ? (w - h) / 2 : (h - w) / 2)); graphics2d.drawimage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; } /** * 图片左转90度 * * @param bufferedimage * @return */ public static bufferedimage rotateimageleft90(bufferedimage bufferedimage) { int w = bufferedimage.getwidth(); int h = bufferedimage.getheight(); int type = bufferedimage.getcolormodel().gettransparency(); bufferedimage img; graphics2d graphics2d; (graphics2d = (img = new bufferedimage(h, w, type)).creategraphics()).setrenderinghint( renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); graphics2d.rotate(math.toradians(270), w / 2, h / 2 + (w - h) / 2); graphics2d.drawimage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; } /** * 图片右转90度 * * @param bufferedimage * @return */ public static bufferedimage rotateimageright90(bufferedimage bufferedimage) { int w = bufferedimage.getwidth(); int h = bufferedimage.getheight(); int type = bufferedimage.getcolormodel().gettransparency(); bufferedimage img; graphics2d graphics2d; (graphics2d = (img = new bufferedimage(h, w, type)).creategraphics()).setrenderinghint( renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); graphics2d.rotate(math.toradians(90), w / 2 - (w - h) / 2, h / 2); graphics2d.drawimage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; } // 对转 public file rotateimageoppo(file file) throws exception { bufferedimage bufferedimage = imageio.read(file); int w = bufferedimage.getwidth(); int h = bufferedimage.getheight(); int type = bufferedimage.getcolormodel().gettransparency(); bufferedimage img; graphics2d graphics2d; (graphics2d = (img = new bufferedimage(w, h, type)).creategraphics()).setrenderinghint( renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); graphics2d.rotate(math.toradians(180), w / 2, h / 2); graphics2d.drawimage(bufferedimage, 0, 0, null); graphics2d.dispose(); imageio.write(img, "jpg", file); return file; } /*** * 图片镜像处理 * * @param file * @param fx * 0 为上下反转 1 为左右反转 * @return */ public void imagemisro(file file, int fx) { try { bufferedimage bufferedimage = imageio.read(file); int w = bufferedimage.getwidth(); int h = bufferedimage.getheight(); int[][] datas = new int[w][h]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { datas[j][i] = bufferedimage.getrgb(j, i); } } int[][] tmps = new int[w][h]; if (fx == 0) { for (int i = 0, a = h - 1; i < h; i++, a--) { for (int j = 0; j < w; j++) { tmps[j][a] = datas[j][i]; } } } else if (fx == 1) { for (int i = 0; i < h; i++) { for (int j = 0, b = w - 1; j < w; j++, b--) { tmps[b][i] = datas[j][i]; } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { bufferedimage.setrgb(j, i, tmps[j][i]); } } imageio.write(bufferedimage, "jpg", file); } catch (exception e) { e.printstacktrace(); } } /** * 对图片进行强制放大或缩小 * * @param originalimage * 原始图片 * @return */ public static bufferedimage zoominimage(bufferedimage originalimage, int width, int height) { bufferedimage newimage = new bufferedimage(width, height, originalimage.gettype()); graphics g = newimage.getgraphics(); g.drawimage(originalimage, 0, 0, width, height, null); g.dispose(); return newimage; } /** * 简易图片识别原理 * * @param img * 图片路径 */ public static void discernimg(string img) { try { file fileone = new file(img); bufferedimage bi = imageio.read(fileone); // 获取图像的宽度和高度 int width = bi.getwidth(); int height = bi.getheight(); // 扫描图片 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) {// 行扫描 int dip = bi.getrgb(j, i); if (dip == -1) system.out.print(" "); else system.out.print("♦"); } system.out.println();// 换行 } } catch (exception e) { logger.error("图片识别出错", e); } } }
更多java相关内容感兴趣的读者可查看本站专题:《java图片操作技巧汇总》、《java日期与时间操作技巧汇总》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》及《java数据结构与算法教程》。
希望本文所述对大家java程序设计有所帮助。