本文实例讲述了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实现的微信图片处理工具类【裁剪,合并,等比例缩放等】...相关推荐

  1. java中集合类的转换_Java中的两个常用工具类及集合数组的相互转换

    为了编程人员的方便及处理数据的安全性,Java特别提供了两个非常有用的工具类: 一.Collections 1.Collections类的特点: 集合框架的工具类.里面定义的都是静态方法. 2.Col ...

  2. java aes 工具类_Java中的AES加解密工具类:AESUtils

    本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...

  3. java 图片合成 红色失真_Java - 处理某些图片泛红

    参考博文: http://blog.csdn.net/kobejayandy/article/details/44346809 http://blog.csdn.net/shixing_11/arti ...

  4. java扫描指定package注解_java随笔-扫描使用指定注解的类与方法

    前几天项目中让扫描出所有使用Restful API的方法.刚开始还想着用python过滤关键字来查找的,后来想想可以使用反射来搞的.主要包含以下三个步骤: 根据包名解析包的具体路径 查找指定包下指定注 ...

  5. java web 加载类_Java中用ClassLoader载入各种资源(类、文件、web资源)的方法

    lassLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回这个类的class对象. ClassLoader负责载入系 ...

  6. java基础教程 类_Java零基础系列教程06初识类和对象

    配套视频教程 万物皆对象 分类是人们认识世界的一个很自然的过程,在日常生活中会不自觉地进行分类 身边的对象 顾客 姓名-张浩 年龄-20 体重-60kg 操作: 购买商品 收银员 员工号-10001 ...

  7. 视频和图片合成软件,简单快速合成视频和图片

    怎么把小视频和图片合成起来?有简单好上手的教程吗?今天就教大家简单几步,把视频和图片合成为照片视频.先看看用数码大师合成视频和图片的效果截图: 第一步:把图片一次性导入,为照片配上文字 点击" ...

  8. php 图片合成gif,php将多张jpg图片生成gif动态图片的代码

    /* 图片合并需要 1   下载   php_imagick_st-Q8.dll 并将其放入 php/ext/ 2 在php.ini 中加入 extension=php_imagick_st-Q8.d ...

  9. java 微信图片盗链破解_隐藏图片地址,伪造来路Referer实现绕过微信图片的防盗链机制...

    最近用软件采集微信公众号文章图片下载不了,全是显示盗链图片.没有办法只好上网查询有没有办法解决.在一个朋友那看到了2种解决方案记录下: 微信防盗链 用php写一个绕过的代码,你只需要把php上传到你的 ...

最新文章

  1. ant 修改组件默认样式属性
  2. 800名科学家Nature联名发文主张废除p值!
  3. windows AD/DNS服务器搭建
  4. 21天的你长这样?剑桥博士小姐姐研究「人造胚胎」,2篇Nature到手
  5. C#中的深克隆的两种方式
  6. 内核代号101 — 动手写自己的内核
  7. kafka删除队列_没想到 Kafka 还会这样问,学会这些带你轻松搞定大厂面试!
  8. 处理模型——通过扩展模型处理器直接处理每个ModelMesh的顶点位置数据
  9. 基于神经网络的实体识别和关系抽取联合学习
  10. 扩展DOM有什么问题
  11. csdn编辑器公式中插入空格
  12. mfc 请求java_MFC使用WinHttp实现Http访问
  13. lnmp一键安装-HTTP ERROR 500目前无法处理此请求
  14. 微信小程序云数据库操作
  15. 扩展卡尔曼滤波soc估算 基于EKF算法的锂电池SOC
  16. 【科普】波特率和比特速率的理解
  17. Configure Python interpreter
  18. [转载] 古稀之年被判无期,84 岁再成亿万富翁,一代商业传奇落幕!
  19. c语言36块砖36人搬答案编程,在C语言中.36块砖,36人搬;男搬4,女搬3,两个小孩抬一块.要求一次搬完,问男女小孩各多少?设计程序....
  20. python: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization问题解决

热门文章

  1. 蒙城县有4000多年,夏为禹贡豫州之域
  2. Excel VBA 小程序 - 文本型数字转为数值型数字
  3. 横河变送器EJA530E-JAS4N-017NN选型参考
  4. 0x300-从头开始写操作系统-内核
  5. spring boot(四):thymeleaf使用详解
  6. 联通查询套餐及名下联通卡
  7. PDF转word之后的结果事图片格式,如何改成.doc或.docx格式
  8. YoLoV5学习(4)--detect.py程序(预测图片、视频、网络流)逐段讲解~
  9. 联想Y430P换屏记
  10. <ESP8266>WIFI模块AT指令浅学