1. 程式人生 > >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; /** * 微信圖片處理工具 * * @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); } } }