您好,登錄后才能下訂單哦!
這篇文章主要介紹了利用java如何實(shí)現(xiàn)一個(gè)圖片縮放效果,億速云小編覺(jué)得不錯(cuò),現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨億速云小編來(lái)看看吧!
Java是一門(mén)面向?qū)ο缶幊陶Z(yǔ)言,可以編寫(xiě)桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
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; // 原始圖片打開(kāi)路徑 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è)矩形的長(zhǎng)寬 * @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);*/ } }
以上就是億速云小編為大家收集整理的利用java如何實(shí)現(xiàn)一個(gè)圖片縮放效果,如何覺(jué)得億速云網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將億速云網(wǎng)站推薦給身邊好友。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。