栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现的微信图片处理工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现的微信图片处理工具类

本文实例讲述了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;

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);// 写图片
  }
  
  private final static int y_width = 645;
  
  private final static int y_height = 920;
  
  private final static int x_retract = 50;
  
  private final static int y_retract = 50;
  
  public final static int BORDER = 20;
  
  public static void xPic(String first, String second, String out) {
   try {
     
     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);
     
     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);
   }
  }
  
  public static boolean yPic(String first, String second, String out, int border) {
   boolean isOk = true;
   try {
     
     File fileOne = new File(first);
     BufferedImage imageFirst = ImageIO.read(fileOne);
     int width = imageFirst.getWidth();// 图片宽度
     int height = imageFirst.getHeight();// 图片高度
     
     File fileTwo = new File(second);
     BufferedImage imageSecond = ImageIO.read(fileTwo);
     int widthTwo = imageSecond.getWidth();// 图片宽度
     int heightTwo = imageSecond.getHeight();// 图片高度
     
     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));
     
     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;
  }
  
  public static boolean maigaoPic(String source, String out, int border) {
   boolean isOk = true;
   try {
     
     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;
  }
  
  public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
   int width = source.getWidth();// 图片宽度
   int height = source.getHeight();// 图片高度
   return zoomInImage(source, targetW, targetH);
   // 图片宽高都太小时,强制放大图片
   
  }
  
  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;
  }
  
  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;
  }
  
  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;
  }
  
  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;
  }
  
  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();
   }
  }
  
  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;
  }
  
  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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142943.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号