java 图片处理
1 /* 2 * 图片处理类 3 */ 4 package image; 5 6 import com.sun.image.codec.jpeg.JPEGCodec; 7 import com.sun.image.codec.jpeg.JPEGEncodeParam; 8 import com.sun.image.codec.jpeg.JPEGImageEncoder; 9 import java.awt.AlphaComposite; 10 import java.awt.Color; 11 import java.awt.Graphics2D; 12 import java.awt.Image; 13 import java.awt.Rectangle; 14 import java.awt.RenderingHints; 15 import java.awt.Transparency; 16 import java.awt.geom.Area; 17 import java.awt.geom.RoundRectangle2D; 18 import java.awt.image.BufferedImage; 19 import java.io.File; 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 import javax.imageio.ImageIO; 23 24 /** 25 * 26 * @author sanshizi 27 */ 28 public class ImageUtil { 29 30 /** 31 * 针对高度与宽度进行等比缩放 32 * 33 * @param img 34 * @param maxSize 要缩放到的尺寸 35 * @param type 1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放 36 * @return 37 */ 38 private static Image getScaledImage(BufferedImage img, int maxSize, int type) { 39 int w0 = img.getWidth(); 40 int h0 = img.getHeight(); 41 int w = w0; 42 int h = h0; 43 if (type == 1) { 44 w = w0 > h0 ? maxSize : (maxSize * w0 / h0); 45 h = w0 > h0 ? (maxSize * h0 / w0) : maxSize; 46 } else if (type == 2) { 47 w = w0 > h0 ? (maxSize * w0 / h0) : maxSize; 48 h = w0 > h0 ? maxSize : (maxSize * h0 / w0); 49 } 50 Image image = img.getScaledInstance(w, h, Image.SCALE_SMOOTH); 51 BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 52 Graphics2D g = result.createGraphics(); 53 g.drawImage(image, 0, 0, null);//在适当的位置画出 54 return result; 55 } 56 57 /** 58 * 先按最小宽高为size等比例绽放, 然后图像居中抠出直径为size的圆形图像 59 * 60 * @param img 61 * @param size 62 * @return 63 */ 64 private static BufferedImage getRoundedImage(BufferedImage img, int size) { 65 return getRoundedImage(img, size, size / 2, 2); 66 } 67 68 /** 69 * 先按最小宽高为size等比例绽放, 然后图像居中抠出半径为radius的圆形图像 70 * 71 * @param img 72 * @param size 要缩放到的尺寸 73 * @param radius 圆角半径 74 * @param type 1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放 75 * @return 76 */ 77 private static BufferedImage getRoundedImage(BufferedImage img, int size, int radius, int type) { 78 79 BufferedImage result = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); 80 Graphics2D g = result.createGraphics(); 81 82 //先按最小宽高为size等比例绽放, 然后图像居中抠出直径为size的圆形图像 83 Image fixedImg = getScaledImage(img, size, type); 84 g.drawImage(fixedImg, (size - fixedImg.getWidth(null)) / 2, (size - fixedImg.getHeight(null)) / 2, null);//在适当的位置画出 85 86 //圆角 87 if (radius > 0) { 88 RoundRectangle2D round = new RoundRectangle2D.Double(0, 0, size, size, radius * 2, radius * 2); 89 Area clear = new Area(new Rectangle(0, 0, size, size)); 90 clear.subtract(new Area(round)); 91 g.setComposite(AlphaComposite.Clear); 92 93 //抗锯齿 94 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 95 g.fill(clear); 96 g.dispose(); 97 } 98 return result; 99 } 100 101 /** 102 * 使用删除alpha值的方式去掉图像的alpha通道 103 * 104 * @param $image 105 * @return 106 */ 107 protected static BufferedImage get24BitImage(BufferedImage $image) { 108 int __w = $image.getWidth(); 109 int __h = $image.getHeight(); 110 int[] __imgARGB = getRGBs($image.getRGB(0, 0, __w, __h, null, 0, __w)); 111 BufferedImage __newImg = new BufferedImage(__w, __h, BufferedImage.TYPE_INT_RGB); 112 __newImg.setRGB(0, 0, __w, __h, __imgARGB, 0, __w); 113 return __newImg; 114 } 115 116 /** 117 * 使用绘制的方式去掉图像的alpha值 118 * 119 * @param $image 120 * @param $bgColor 121 * @return 122 */ 123 protected static BufferedImage get24BitImage(BufferedImage $image, Color $bgColor) { 124 int $w = $image.getWidth(); 125 int $h = $image.getHeight(); 126 BufferedImage img = new BufferedImage($w, $h, BufferedImage.TYPE_INT_RGB); 127 Graphics2D g = img.createGraphics(); 128 g.setColor($bgColor); 129 g.fillRect(0, 0, $w, $h); 130 g.drawRenderedImage($image, null); 131 g.dispose(); 132 return img; 133 } 134 135 /** 136 * 将32位色彩转换成24位色彩(丢弃Alpha通道) 137 * 138 * @param $argb 139 * @return 140 */ 141 public static int[] getRGBs(int[] $argb) { 142 int[] __rgbs = new int[$argb.length]; 143 for (int i = 0; i < $argb.length; i++) { 144 __rgbs[i] = $argb[i] & 0xFFFFFF; 145 } 146 return __rgbs; 147 } 148 149 public static void toJPG(File img, File save, int size, int quality) throws IOException { 150 FileOutputStream out = new FileOutputStream(save); 151 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 152 153 BufferedImage image = (BufferedImage) getRoundedImage(ImageIO.read(img), size, 0, 2);//默认无圆角 154 155 //如果图像是透明的,就丢弃Alpha通道 156 if (image.getTransparency() == Transparency.TRANSLUCENT) { 157 image = get24BitImage(image); 158 } 159 160 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);//使用jpeg编码器 161 param.setQuality(1, true);//高质量jpg图片输出 162 encoder.encode(image, param); 163 164 out.close(); 165 } 166 167 public static void toPNG(File img, File save, int size) throws IOException { 168 ImageIO.write((BufferedImage) getRoundedImage(ImageIO.read(img), size, 0, 2), "PNG", save);//默认无圆角 169 } 170 171 public static void main(String[] args) throws IOException { 172 File img = new File("e:\\Users\\rocky\\Desktop\\0\\IMG_0404.PNG"); 173 File save = new File("e:\\Users\\rocky\\Desktop\\0\\zz.jpg"); 174 175 toJPG(img, save, 250, 100); 176 } 177 }