java實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)和馬賽克化
更新時(shí)間:2022年04月22日 14:53:54 作者:Crazy光光
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)和馬賽克化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文是作者結(jié)合網(wǎng)上的一些資料封裝的java圖片處理類,支持圖片的縮放,旋轉(zhuǎn),馬賽克化。
不多說,上代碼:
package deal; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; /** * 圖像處理類. * * @author nagsh * */ public class ImageDeal { String openUrl; // 原始圖片打開路徑 String saveUrl; // 新圖保存路徑 String saveName; // 新圖名稱 String suffix; // 新圖類型 只支持gif,jpg,png public ImageDeal(String openUrl, String saveUrl, String saveName, String suffix) { this.openUrl = openUrl; this.saveName = saveName; this.saveUrl = saveUrl; this.suffix = suffix; } /** * 圖片縮放. * * @param width * 需要的寬度 * @param height * 需要的高度 * @throws Exception */ public void zoom(int width, int height) throws Exception { double sx = 0.0; double sy = 0.0; File file = new File(openUrl); if (!file.isFile()) { throw new Exception("ImageDeal>>>" + file + " 不是一個(gè)圖片文件!"); } BufferedImage bi = ImageIO.read(file); // 讀取該圖片 // 計(jì)算x軸y軸縮放比例--如需等比例縮放,在調(diào)用之前確保參數(shù)width和height是等比例變化的 sx = (double) width / bi.getWidth(); sy = (double) height / bi.getHeight(); AffineTransformOp op = new AffineTransformOp( AffineTransform.getScaleInstance(sx, sy), null); File sf = new File(saveUrl, saveName + "." + suffix); Image zoomImage = op.filter(bi, null); try { ImageIO.write((BufferedImage) zoomImage, suffix, sf); // 保存圖片 } catch (Exception e) { e.printStackTrace(); } } /** * 旋轉(zhuǎn) * * @param degree * 旋轉(zhuǎn)角度 * @throws Exception */ public void spin(int degree) throws Exception { int swidth = 0; // 旋轉(zhuǎn)后的寬度 int sheight = 0; // 旋轉(zhuǎn)后的高度 int x; // 原點(diǎn)橫坐標(biāo) int y; // 原點(diǎn)縱坐標(biāo) File file = new File(openUrl); if (!file.isFile()) { throw new Exception("ImageDeal>>>" + file + " 不是一個(gè)圖片文件!"); } BufferedImage bi = ImageIO.read(file); // 讀取該圖片 // 處理角度--確定旋轉(zhuǎn)弧度 degree = degree % 360; if (degree < 0) degree = 360 + degree;// 將角度轉(zhuǎn)換到0-360度之間 double theta = Math.toRadians(degree);// 將角度轉(zhuǎn)為弧度 // 確定旋轉(zhuǎn)后的寬和高 if (degree == 180 || degree == 0 || degree == 360) { swidth = bi.getWidth(); sheight = bi.getHeight(); } else if (degree == 90 || degree == 270) { sheight = bi.getWidth(); swidth = bi.getHeight(); } else { swidth = (int) (Math.sqrt(bi.getWidth() * bi.getWidth() + bi.getHeight() * bi.getHeight())); sheight = (int) (Math.sqrt(bi.getWidth() * bi.getWidth() + bi.getHeight() * bi.getHeight())); } x = (swidth / 2) - (bi.getWidth() / 2);// 確定原點(diǎn)坐標(biāo) y = (sheight / 2) - (bi.getHeight() / 2); BufferedImage spinImage = new BufferedImage(swidth, sheight, bi.getType()); // 設(shè)置圖片背景顏色 Graphics2D gs = (Graphics2D) spinImage.getGraphics(); gs.setColor(Color.white); gs.fillRect(0, 0, swidth, sheight);// 以給定顏色繪制旋轉(zhuǎn)后圖片的背景 AffineTransform at = new AffineTransform(); at.rotate(theta, swidth / 2, sheight / 2);// 旋轉(zhuǎn)圖象 at.translate(x, y); AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC); spinImage = op.filter(bi, spinImage); File sf = new File(saveUrl, saveName + "." + suffix); ImageIO.write(spinImage, suffix, sf); // 保存圖片 } /** * 馬賽克化. * @param size 馬賽克尺寸,即每個(gè)矩形的長寬 * @return * @throws Exception */ public boolean mosaic(int size) throws Exception { File file = new File(openUrl); if (!file.isFile()) { throw new Exception("ImageDeal>>>" + file + " 不是一個(gè)圖片文件!"); } BufferedImage bi = ImageIO.read(file); // 讀取該圖片 BufferedImage spinImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.TYPE_INT_RGB); if (bi.getWidth() < size || bi.getHeight() < size || size <= 0) { // 馬賽克格尺寸太大或太小 return false; } int xcount = 0; // 方向繪制個(gè)數(shù) int ycount = 0; // y方向繪制個(gè)數(shù) if (bi.getWidth() % size == 0) { xcount = bi.getWidth() / size; } else { xcount = bi.getWidth() / size + 1; } if (bi.getHeight() % size == 0) { ycount = bi.getHeight() / size; } else { ycount = bi.getHeight() / size + 1; } int x = 0; //坐標(biāo) int y = 0; // 繪制馬賽克(繪制矩形并填充顏色) Graphics gs = spinImage.getGraphics(); for (int i = 0; i < xcount; i++) { for (int j = 0; j < ycount; j++) { //馬賽克矩形格大小 int mwidth = size; int mheight = size; if(i==xcount-1){ //橫向最后一個(gè)比較特殊,可能不夠一個(gè)size mwidth = bi.getWidth()-x; } if(j == ycount-1){ //同理 mheight =bi.getHeight()-y; } // 矩形顏色取中心像素點(diǎn)RGB值 int centerX = x; int centerY = y; if (mwidth % 2 == 0) { centerX += mwidth / 2; } else { centerX += (mwidth - 1) / 2; } if (mheight % 2 == 0) { centerY += mheight / 2; } else { centerY += (mheight - 1) / 2; } Color color = new Color(bi.getRGB(centerX, centerY)); gs.setColor(color); gs.fillRect(x, y, mwidth, mheight); y = y + size;// 計(jì)算下一個(gè)矩形的y坐標(biāo) } y = 0;// 還原y坐標(biāo) x = x + size;// 計(jì)算x坐標(biāo) } gs.dispose(); File sf = new File(saveUrl, saveName + "." + suffix); ImageIO.write(spinImage, suffix, sf); // 保存圖片 return true; } public static void main(String[] args) throws Exception { ImageDeal imageDeal = new ImageDeal("e://1.jpg", "e://", "2", "jpg"); // 測(cè)試縮放 /* imageDeal.zoom(200, 300); */ // 測(cè)試旋轉(zhuǎn) /* imageDeal.spin(90); */ //測(cè)試馬賽克 /*imageDeal.mosaic(4);*/ } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Java實(shí)現(xiàn)圖片比率縮放
- java 實(shí)現(xiàn)圖片像素質(zhì)量壓縮與圖片長寬縮放
- java高質(zhì)量縮放圖片的示例代碼
- Java實(shí)現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例
- Java實(shí)現(xiàn)的微信圖片處理工具類【裁剪,合并,等比例縮放等】
- java對(duì)圖片進(jìn)行壓縮和resize縮放的方法
- java圖片縮放實(shí)現(xiàn)圖片填充整個(gè)屏幕
- Java圖片處理 (文字水印、圖片水印、縮放、補(bǔ)白)代碼實(shí)例
- 簡單的java圖片處理類(圖片水印 圖片縮放)
- java項(xiàng)目實(shí)現(xiàn)圖片等比縮放
相關(guān)文章
如何基于Java實(shí)現(xiàn)對(duì)象List排序
這篇文章主要介紹了如何基于Java實(shí)現(xiàn)對(duì)象List排序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01swagger2隱藏在API文檔顯示某些參數(shù)的操作
這篇文章主要介紹了swagger2隱藏在API文檔顯示某些參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06String字符串轉(zhuǎn)BigDecimal時(shí),報(bào)NumberFormatException異常的解決
這篇文章主要介紹了String字符串轉(zhuǎn)BigDecimal時(shí),報(bào)NumberFormatException異常的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringMVC學(xué)習(xí)之JSON和全局異常處理詳解
在項(xiàng)目上線之后,往往會(huì)出現(xiàn)一些不可預(yù)料的異常信息,對(duì)于邏輯性或設(shè)計(jì)性問題,開發(fā)人員或者維護(hù)人員需要通過日志,查看異常信息并排除異常,這篇文章主要給大家介紹了關(guān)于SpringMVC學(xué)習(xí)之JSON和全局異常處理的相關(guān)資料,需要的朋友可以參考下2022-10-10