java 图片 g_java 图片处理

1 /*

2 * 图片处理类3 */

4 packageimage;5

6 importcom.sun.image.codec.jpeg.JPEGCodec;7 importcom.sun.image.codec.jpeg.JPEGEncodeParam;8 importcom.sun.image.codec.jpeg.JPEGImageEncoder;9 importjava.awt.AlphaComposite;10 importjava.awt.Color;11 importjava.awt.Graphics2D;12 importjava.awt.Image;13 importjava.awt.Rectangle;14 importjava.awt.RenderingHints;15 importjava.awt.Transparency;16 importjava.awt.geom.Area;17 importjava.awt.geom.RoundRectangle2D;18 importjava.awt.image.BufferedImage;19 importjava.io.File;20 importjava.io.FileOutputStream;21 importjava.io.IOException;22 importjavax.imageio.ImageIO;23

24 /**

25 *26 *@authorsanshizi27 */

28 public classImageUtil {29

30 /**

31 * 针对高度与宽度进行等比缩放32 *33 *@paramimg34 *@parammaxSize 要缩放到的尺寸35 *@paramtype 1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放36 *@return

37 */

38 private static Image getScaledImage(BufferedImage img, int maxSize, inttype) {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 = newBufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);52 Graphics2D g =result.createGraphics();53 g.drawImage(image, 0, 0, null);//在适当的位置画出

54 returnresult;55 }56

57 /**

58 * 先按最小宽高为size等比例绽放, 然后图像居中抠出直径为size的圆形图像59 *60 *@paramimg61 *@paramsize62 *@return

63 */

64 private static BufferedImage getRoundedImage(BufferedImage img, intsize) {65 return getRoundedImage(img, size, size / 2, 2);66 }67

68 /**

69 * 先按最小宽高为size等比例绽放, 然后图像居中抠出半径为radius的圆形图像70 *71 *@paramimg72 *@paramsize 要缩放到的尺寸73 *@paramradius 圆角半径74 *@paramtype 1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放75 *@return

76 */

77 private static BufferedImage getRoundedImage(BufferedImage img, int size, int radius, inttype) {78

79 BufferedImage result = newBufferedImage(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(newArea(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 returnresult;99 }100

101 /**

102 * 使用删除alpha值的方式去掉图像的alpha通道103 *104 *@param$image105 *@return

106 */

107 protected staticBufferedImage 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 = newBufferedImage(__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$image120 *@param$bgColor121 *@return

122 */

123 protected staticBufferedImage get24BitImage(BufferedImage $image, Color $bgColor) {124 int $w =$image.getWidth();125 int $h =$image.getHeight();126 BufferedImage img = newBufferedImage($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 returnimg;133 }134

135 /**

136 * 将32位色彩转换成24位色彩(丢弃Alpha通道)137 *138 *@param$argb139 *@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) throwsIOException {150 FileOutputStream out = newFileOutputStream(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) throwsIOException {168 ImageIO.write((BufferedImage) getRoundedImage(ImageIO.read(img), size, 0, 2), "PNG", save);//默认无圆角

169 }170

171 public static void main(String[] args) throwsIOException {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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值