阅读背景:

工具类:图片

来源:互联网 
开始 package com.xjxcc.util; import java.awt.AlphaComposite; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigDecimal; import javax.imageio.ImageIO; import javax.swing.ImageIcon; public class ImageUtils { /** 图片类型:png格式 */ public static final String IMG_TYPE_PNG = "png"; /** 图片类型:jpg格式 */ public static final String IMG_TYPE_JPG = "jpg"; /** 图片类型:gif格式 */ public static final String IMG_TYPE_GIF = "gif"; /** 拼接图片类型:横向X拼接 */ public static final int MERGE_IMG_TYPE_X = 1 ; /** 拼接图片类型:横向Y拼接 */ public static final int MERGE_IMG_TYPE_Y = 2 ; /** * @Title: mergeImg * @Description: { 拼接多张图片 }(这里用一句话描述这个方法的作用) * @param imgsFiles 多张图片的file * @param type 生成图片类型(png、jpg) * @param dst_pic 目标图片路径 * @param type 拼接类型 1、横向X拼接 2、纵向Y拼接 * @throws */ public static boolean mergeImg(File[] imgsFiles, String imgType, int mergeType, String dstPic) { int len = imgsFiles.length; // 判断长度是否大于0 if (len < 1) { return false; } BufferedImage[] images = new BufferedImage[len]; int[][] ImageArrays = new int[len][]; for (int i = 0; i < len; i++) { try { images[i] = ImageIO.read(imgsFiles[i]); } catch (Exception e) { e.printStackTrace(); return false; } int width = images[i].getWidth(); int height = images[i].getHeight(); // 从图片中读取RGB 像素 ImageArrays[i] = new int[width * height]; ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width); } int dst_height = 0, dst_width = 0, max_dst_width = 0; if(mergeType == MERGE_IMG_TYPE_Y){ dst_width = images[0].getWidth(); }else if(mergeType == MERGE_IMG_TYPE_X){ dst_height = images[0].getHeight(); } // 合成图片像素 for (int i = 0; i < images.length; i++) { if(mergeType == MERGE_IMG_TYPE_Y){ dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth(); dst_height += images[i].getHeight(); }else if(mergeType == MERGE_IMG_TYPE_X){ dst_height = dst_height > images[i].getHeight() ? dst_height : images[i].getHeight(); max_dst_width = max_dst_width > images[i].getWidth() ? max_dst_width : images[i].getWidth(); dst_width += images[i].getWidth(); } } // 合成后的图片 if (dst_height < 1 && dst_width < 1) { return false; } // 生成新图片 try { BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB); int height_i = 0, width_i = 0; for (int i = 0; i < images.length; i++) { if(mergeType == MERGE_IMG_TYPE_Y){ ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width); height_i += images[i].getHeight(); }else if(mergeType == MERGE_IMG_TYPE_X){ ImageNew.setRGB(width_i, 0, images[i].getWidth(), dst_height, ImageArrays[i], 0, max_dst_width); width_i += images[i].getWidth(); } } File outFile = new File(dstPic); ImageIO.write(ImageNew, imgType, outFile);// 写图片 ,输出到硬盘 } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * * @Title: changeImgSize * @Description: { 改变图片的大小 }(这里用一句话描述这个方法的作用) * @param imgFile 图片 * @param newImgFile 新图片 * @param newImgWidth 新图片的宽度 * @param newImgHeight 新图片的高度 * @throws */ public static void changeImgSize (File imgFile , File newImgFile , Integer newImgWidth , Integer newImgHeight){ newImgWidth = (newImgWidth == null ? 400 : newImgWidth); newImgHeight = (newImgHeight == null ? 400 : newImgHeight); BufferedImage bimg; try { bimg = ImageIO.read(imgFile); Image image = bimg.getScaledInstance(newImgWidth, newImgHeight, Image.SCALE_SMOOTH); BufferedImage target = new BufferedImage(newImgWidth, newImgHeight, BufferedImage.TYPE_INT_BGR); Graphics g = target.getGraphics(); g.drawImage(image, BigDecimal.ZERO.intValue(), BigDecimal.ZERO.intValue(), null); g.dispose(); ImageIO.write(target, "jpg", newImgFile); } catch (IOException e) { e.printStackTrace(); } } /** * * @Title: markImageByIcon * @Description: { 给图片添加水印、可设置水印图片旋转角度 }(这里用一句话描述这个方法的作用) * @param iconPath 水印图片路径 * @param srcImgPath 源图片路径 * @param targerPath 目标图片路径 * @param degree 水印图片旋转角度 * @throws */ public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 得到画笔对象 Graphics2D g = buffImg.createGraphics(); // 设置对线段的锯齿状边缘处理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); if (null != degree) { // 设置水印旋转 g.rotate(Math.toRadians(degree),(double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } // 水印图象的路径 水印一般为gif或者png的,这样可设置透明度 ImageIcon imgIcon = new ImageIcon(iconPath); // 得到Image对象。 Image img = imgIcon.getImage(); float alpha = 0.7f; // 透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha)); // 表示水印图片的位置 g.drawImage(img, (srcImg.getWidth(null) > img.getWidth(null)?srcImg.getWidth(null) - img.getWidth(null) : 0), (srcImg.getHeight(null) > img.getHeight(null)?srcImg.getHeight(null) - img.getHeight(null) : 0) , null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); os = new FileOutputStream(targerPath); // 生成图片 ImageIO.write(buffImg, "JPG", os); System.out.println("图片完成添加Icon印章。。。。。。"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } } 结束 开始 package com.xjxcc.util; import java.awt.AlphaC



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: