java 圖片處理類, PNG, JPG 互轉, 支援圓角, 等比摳正方形圖
阿新 • • 發佈:2019-01-29
綜合網上刪除Alpha通道的內容, 修改出來的版本, 勉強算原創吧
注意點: 這裡所有方法都是不支援多執行緒的(因為ImageIO)
[java] view plain copy print?- /*
- * 圖片處理類
- */
- package image;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGEncodeParam;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- import java.awt.AlphaComposite;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.RenderingHints;
- import java.awt.Transparency;
- import java.awt.geom.Area;
- import java.awt.geom.RoundRectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import
- import java.io.IOException;
- import javax.imageio.ImageIO;
- /**
- *
- * @author sanshizi
- */
- publicclass ImageUtil {
- /**
- * 針對高度與寬度進行等比縮放
- *
- * @param img
- * @param maxSize 要縮放到的尺寸
- * @param type 1:高度與寬度的最大值為maxSize進行等比縮放 , 2:高度與寬度的最小值為maxSize進行等比縮放
- * @return
- */
- privatestatic Image getScaledImage(BufferedImage img, int maxSize, int type) {
- int w0 = img.getWidth();
- int h0 = img.getHeight();
- int w = w0;
- int h = h0;
- if (type == 1) {
- w = w0 > h0 ? maxSize : (maxSize * w0 / h0);
- h = w0 > h0 ? (maxSize * h0 / w0) : maxSize;
- } elseif (type == 2) {
- w = w0 > h0 ? (maxSize * w0 / h0) : maxSize;
- h = w0 > h0 ? maxSize : (maxSize * h0 / w0);
- }
- Image image = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
- BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g = result.createGraphics();
- g.drawImage(image, 0, 0, null);//在適當的位置畫出
- return result;
- }
- /**
- * 先按最小寬高為size等比例綻放, 然後影象居中摳出直徑為size的圓形影象
- *
- * @param img
- * @param size
- * @return
- */
- privatestatic BufferedImage getRoundedImage(BufferedImage img, int size) {
- return getRoundedImage(img, size, size / 2, 2);
- }
- /**
- * 先按最小寬高為size等比例綻放, 然後影象居中摳出半徑為radius的圓形影象
- *
- * @param img
- * @param size 要縮放到的尺寸
- * @param radius 圓角半徑
- * @param type 1:高度與寬度的最大值為maxSize進行等比縮放 , 2:高度與寬度的最小值為maxSize進行等比縮放
- * @return
- */
- privatestatic BufferedImage getRoundedImage(BufferedImage img, int size, int radius, int type) {
- BufferedImage result = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g = result.createGraphics();
- //先按最小寬高為size等比例綻放, 然後影象居中摳出直徑為size的圓形影象
- Image fixedImg = getScaledImage(img, size, type);
- g.drawImage(fixedImg, (size - fixedImg.getWidth(null)) / 2, (size - fixedImg.getHeight(null)) / 2, null);//在適當的位置畫出
- //圓角
- if (radius > 0) {
- RoundRectangle2D round = new RoundRectangle2D.Double(0, 0, size, size, radius * 2, radius * 2);
- Area clear = new Area(new Rectangle(0, 0, size, size));
- clear.subtract(new Area(round));
- g.setComposite(AlphaComposite.Clear);
- //抗鋸齒
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.fill(clear);
- g.dispose();
- }
- return result;
- }
- /**
- * 使用刪除alpha值的方式去掉影象的alpha通道
- *
- * @param $image
- * @return
- */
- protectedstatic BufferedImage get24BitImage(BufferedImage $image) {
- int __w = $image.getWidth();
- int __h = $image.getHeight();
- int[] __imgARGB = getRGBs($image.getRGB(0, 0, __w, __h, null, 0, __w));
- BufferedImage __newImg = new BufferedImage(__w, __h, BufferedImage.TYPE_INT_RGB);
- __newImg.setRGB(0, 0, __w, __h, __imgARGB, 0, __w);
- return __newImg;
- }
- /**
- * 使用繪製的方式去掉影象的alpha值
- *
- * @param $image
- * @param $bgColor
- * @return
- */
- protectedstatic BufferedImage get24BitImage(BufferedImage $image, Color $bgColor) {
- int $w = $image.getWidth();
- int $h = $image.getHeight();
- BufferedImage img = new BufferedImage($w, $h, BufferedImage.TYPE_INT_RGB);
- Graphics2D g = img.createGraphics();
- g.setColor($bgColor);
- g.fillRect(0, 0, $w, $h);
- g.drawRenderedImage($image, null);
- g.dispose();
- return img;
- }
- /**
- * 將32位色彩轉換成24位色彩(丟棄Alpha通道)
- *
- * @param $argb
- * @return
- */
- publicstaticint[] getRGBs(int[] $argb) {
- int[] __rgbs = newint[$argb.length];
- for (int i = 0; i < $argb.length; i++) {
- __rgbs[i] = $argb[i] & 0xFFFFFF;
- }
- return __rgbs;
- }
- publicstaticvoid toJPG(File img, File save, int size, int quality) throws IOException {
- FileOutputStream out = new FileOutputStream(save);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- BufferedImage image = (BufferedImage) getRoundedImage(ImageIO.read(img), size, 0, 2);//預設無圓角
- //如果影象是透明的,就丟棄Alpha通道
- if (image.getTransparency() == Transparency.TRANSLUCENT) {
- image = get24BitImage(image);
- }
- JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);//使用jpeg編碼器
- param.setQuality(1, true);//高質量jpg圖片輸出
- encoder.encode(image, param);
- out.close();
- }
- publicstaticvoid toPNG(File img, File save, int size) throws IOException {
- ImageIO.write((BufferedImage) getRoundedImage(ImageIO.read(img), size, 0, 2), "PNG", save);//預設無圓角
- }
- publicstaticvoid main(String[] args) throws IOException {
- File img = new File("e:\\Users\\rocky\\Desktop\\0\\IMG_0404.PNG");
- File save = new File("e:\\Users\\rocky\\Desktop\\0\\zz.jpg");
- toJPG(img, save, 250, 100);
- }
- }
/*
* 圖片處理類
*/
package image;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author sanshizi
*/
public class ImageUtil {
/**
* 針對高度與寬度進行等比縮放
*
* @param img
* @param maxSize 要縮放到的尺寸
* @param type 1:高度與寬度的最大值為maxSize進行等比縮放 , 2:高度與寬度的最小值為maxSize進行等比縮放
* @return
*/
private static Image getScaledImage(BufferedImage img, int maxSize, int type) {
int w0 = img.getWidth();
int h0 = img.getHeight();
int w = w0;
int h = h0;
if (type == 1) {
w = w0 > h0 ? maxSize : (maxSize * w0 / h0);
h = w0 > h0 ? (maxSize * h0 / w0) : maxSize;
} else if (type == 2) {
w = w0 > h0 ? (maxSize * w0 / h0) : maxSize;
h = w0 > h0 ? maxSize : (maxSize * h0 / w0);
}
Image image = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
g.drawImage(image, 0, 0, null);//在適當的位置畫出
return result;
}
/**
* 先按最小寬高為size等比例綻放, 然後影象居中摳出直徑為size的圓形影象
*
* @param img
* @param size
* @return
*/
private static BufferedImage getRoundedImage(BufferedImage img, int size) {
return getRoundedImage(img, size, size / 2, 2);
}
/**
* 先按最小寬高為size等比例綻放, 然後影象居中摳出半徑為radius的圓形影象
*
* @param img
* @param size 要縮放到的尺寸
* @param radius 圓角半徑
* @param type 1:高度與寬度的最大值為maxSize進行等比縮放 , 2:高度與寬度的最小值為maxSize進行等比縮放
* @return
*/
private static BufferedImage getRoundedImage(BufferedImage img, int size, int radius, int type) {
BufferedImage result = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
//先按最小寬高為size等比例綻放, 然後影象居中摳出直徑為size的圓形影象
Image fixedImg = getScaledImage(img, size, type);
g.drawImage(fixedImg, (size - fixedImg.getWidth(null)) / 2, (size - fixedImg.getHeight(null)) / 2, null);//在適當的位置畫出
//圓角
if (radius > 0) {
RoundRectangle2D round = new RoundRectangle2D.Double(0, 0, size, size, radius * 2, radius * 2);
Area clear = new Area(new Rectangle(0, 0, size, size));
clear.subtract(new Area(round));
g.setComposite(AlphaComposite.Clear);
//抗鋸齒
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.fill(clear);
g.dispose();
}
return result;
}
/**
* 使用刪除alpha值的方式去掉影象的alpha通道
*
* @param $image
* @return
*/
protected static BufferedImage get24BitImage(BufferedImage $image) {
int __w = $image.getWidth();
int __h = $image.getHeight();
int[] __imgARGB = getRGBs($image.getRGB(0, 0, __w, __h, null, 0, __w));
BufferedImage __newImg = new BufferedImage(__w, __h, BufferedImage.TYPE_INT_RGB);
__newImg.setRGB(0, 0, __w, __h, __imgARGB, 0, __w);
return __newImg;
}
/**
* 使用繪製的方式去掉影象的alpha值
*
* @param $image
* @param $bgColor
* @return
*/
protected static BufferedImage get24BitImage(BufferedImage $image, Color $bgColor) {
int $w = $image.getWidth();
int $h = $image.getHeight();
BufferedImage img = new BufferedImage($w, $h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setColor($bgColor);
g.fillRect(0, 0, $w, $h);
g.drawRenderedImage($image, null);
g.dispose();
return img;
}
/**
* 將32位色彩轉換成24位色彩(丟棄Alpha通道)
*
* @param $argb
* @return
*/
public static int[] getRGBs(int[] $argb) {
int[] __rgbs = new int[$argb.length];
for (int i = 0; i < $argb.length; i++) {
__rgbs[i] = $argb[i] & 0xFFFFFF;
}
return __rgbs;
}
public static void toJPG(File img, File save, int size, int quality) throws IOException {
FileOutputStream out = new FileOutputStream(save);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
BufferedImage image = (BufferedImage) getRoundedImage(ImageIO.read(img), size, 0, 2);//預設無圓角
//如果影象是透明的,就丟棄Alpha通道
if (image.getTransparency() == Transparency.TRANSLUCENT) {
image = get24BitImage(image);
}
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);//使用jpeg編碼器
param.setQuality(1, true);//高質量jpg圖片輸出
encoder.encode(image, param);
out.close();
}
public static void toPNG(File img, File save, int size) throws IOException {
ImageIO.write((BufferedImage) getRoundedImage(ImageIO.read(img), size, 0, 2), "PNG", save);//預設無圓角
}
public static void main(String[] args) throws IOException {
File img = new File("e:\\Users\\rocky\\Desktop\\0\\IMG_0404.PNG");
File save = new File("e:\\Users\\rocky\\Desktop\\0\\zz.jpg");
toJPG(img, save, 250, 100);
}
}