Java實(shí)現(xiàn)的微信圖片處理工具類【裁剪,合并,等比例縮放等】
本文實(shí)例講述了Java實(shí)現(xiàn)的微信圖片處理工具類。分享給大家供大家參考,具體如下:
現(xiàn)在 外面核心,圖片文章比較少,看了拷貝代碼,而用不了,用相應(yīng)jar包處理,很多等比例縮放,達(dá)不到 想要的給予的期望:本工具類,是之前做微信打印機(jī)寫的 基于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坐標(biāo)像素 */ private final static int y_width = 645; /** * 標(biāo)準(zhǔn)圖片的y坐標(biāo)像素,920,是一般照片,1099是郵票照片 */ private final static int y_height = 920; /** * 裁剪x坐標(biāo)縮進(jìn)像素 */ private final static int x_retract = 50; /** * 裁剪y坐標(biāo)縮進(jìn)像素 */ private final static int y_retract = 50; /** * 系統(tǒng)默認(rèn)圖片邊框?yàn)?0 */ 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 對(duì)第二張圖片做相同的處理 */ 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);// 設(shè)置左半部分的RGB imageResult.setRGB(width, 0, widthTwo, heightTwo, imageArraySecond, 0, widthTwo);// 設(shè)置右半部分的RGB File outFile = new File(out); ImageIO.write(imageResult, "jpg", outFile);// 寫圖片 } catch (Exception e) { logger.error("橫向合成圖片出錯(cuò)....", e); } } /** * 縱向合成圖片 * * @param first * 放上面的圖片路徑 * @param second * 放下面的圖片路徑 * @param out * 文件輸出目錄 * @param border * 圖片預(yù)留邊框 */ 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對(duì)第二張圖片做相同的處理 */ 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; // 圖片是橫圖,逆時(shí)針旋轉(zhuǎn)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坐標(biāo) int s_w = 0, s_h = 0; // 裁剪x坐標(biāo)時(shí),縮進(jìn)屬性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坐標(biāo)時(shí),縮進(jìn)屬性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對(duì)第二張圖片做相同的處理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); // 解決黑色背景,默認(rèn)的TYPE_INT_RGB都是0,都是黑色的 Graphics2D g = (Graphics2D) imageResult.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, w, h);// 填充整個(gè)屏幕 g.dispose(); // 留邊框 imageResult.setRGB(border, 0, (width - border * 2), height, imageArrayFirst, 0, (width - border));// 設(shè)置左半部分的RGB imageResult.setRGB(0, height, widthTwo, heightTwo, imageArraySecond, 0, widthTwo);// 設(shè)置右半部分的RGB File outFile = new File(out); ImageIO.write(imageResult, "jpg", outFile);// 寫圖片 } catch (Exception e) { logger.error("縱向合成圖片失敗....", e); isOk = false; } return isOk; } /** * 全圖打印,圖片縮放、旋轉(zhuǎn)處理 * * @param source * 待處理的圖片 * @param out * 處理后文件輸出目錄 * @param border * 圖片預(yù)留邊框 */ 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();// 圖片高度 // 圖片是橫圖,逆時(shí)針旋轉(zhuǎn)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坐標(biāo) int s_w = 0, s_h = 0; // 裁剪x坐標(biāo)時(shí),縮進(jìn)屬性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坐標(biāo)時(shí),縮進(jìn)屬性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); // 解決黑色背景,默認(rèn)的TYPE_INT_RGB都是0,都是黑色的 Graphics2D g = (Graphics2D) imageResult.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height);// 填充整個(gè)屏幕 g.dispose(); // 留邊框 imageResult.setRGB(border, 0, (width - border * 2), height, imageArrayFirst, 0, (width - border));// 設(shè)置左半部分的RGB File outFile = new File(out); ImageIO.write(imageResult, "jpg", outFile);// 寫圖片 } catch (IOException e) { logger.error("全圖打印,圖片縮放、旋轉(zhuǎn)處理失敗....", e); isOk = false; } return isOk; } /** * 實(shí)現(xiàn)圖像的等比縮放 * * @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); // 圖片寬高都太小時(shí),強(qiáng)制放大圖片 /* 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坐標(biāo) * @param startY * 開始y坐標(biāo) * @param endX * 結(jié)束x坐標(biāo) * @param endY * 結(jié)束y坐標(biāo) * @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; } /** * 旋轉(zhuǎn)圖片為指定角度 * * @param bufferedimage * 目標(biāo)圖像 * @param degree * 旋轉(zhuǎn)角度 * @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; } /** * 圖片左轉(zhuǎn)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; } /** * 圖片右轉(zhuǎn)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; } // 對(duì)轉(zhuǎn) 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 為上下反轉(zhuǎn) 1 為左右反轉(zhuǎn) * @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(); } } /** * 對(duì)圖片進(jìn)行強(qiáng)制放大或縮小 * * @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; } /** * 簡(jiǎn)易圖片識(shí)別原理 * * @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("圖片識(shí)別出錯(cuò)", e); } } }
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時(shí)間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Java實(shí)現(xiàn)圖片比率縮放
- java 實(shí)現(xiàn)圖片像素質(zhì)量壓縮與圖片長(zhǎng)寬縮放
- java高質(zhì)量縮放圖片的示例代碼
- java實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)和馬賽克化
- Java實(shí)現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例
- java對(duì)圖片進(jìn)行壓縮和resize縮放的方法
- java圖片縮放實(shí)現(xiàn)圖片填充整個(gè)屏幕
- Java圖片處理 (文字水印、圖片水印、縮放、補(bǔ)白)代碼實(shí)例
- 簡(jiǎn)單的java圖片處理類(圖片水印 圖片縮放)
- java項(xiàng)目實(shí)現(xiàn)圖片等比縮放
相關(guān)文章
Spring Boot應(yīng)用程序同時(shí)支持HTTP和HTTPS協(xié)議的實(shí)現(xiàn)方法
如今,企業(yè)級(jí)應(yīng)用程序的常見場(chǎng)景是同時(shí)支持HTTP和HTTPS兩種協(xié)議,這篇文章考慮如何讓Spring Boot應(yīng)用程序同時(shí)支持HTTP和HTTPS兩種協(xié)議,需要的朋友可以參考下2019-10-10SSM 實(shí)現(xiàn)登錄驗(yàn)證碼功能(附源碼)
這篇文章主要介紹了SSM 實(shí)現(xiàn)登錄驗(yàn)證碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12Java Socket編程心跳包創(chuàng)建實(shí)例解析
這篇文章主要介紹了Java Socket編程心跳包創(chuàng)建實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12Java并發(fā)(Runnable+Thread)實(shí)現(xiàn)硬盤文件搜索功能
這篇文章主要介紹了Java并發(fā)(Runnable+Thread)實(shí)現(xiàn)硬盤文件搜索,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Java如何優(yōu)雅地關(guān)閉資源try-with-resource及其異常抑制
這篇文章主要介紹了Java如何優(yōu)雅地關(guān)閉資源try-with-resource及其異常抑制,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02【Java IO流】字節(jié)流和字符流的實(shí)例講解
下面小編就為大家?guī)硪黄綣ava IO流】字節(jié)流和字符流的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09使用Lombok @Builder注解導(dǎo)致默認(rèn)值無效的問題
這篇文章主要介紹了使用Lombok @Builder注解導(dǎo)致默認(rèn)值無效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08如何使用Spring Validation優(yōu)雅地校驗(yàn)參數(shù)
這篇文章主要介紹了如何使用Spring Validation優(yōu)雅地校驗(yàn)參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07