1 /** 2 * @Description: 将base64编码字符串转换为图片 3 * @param imgStr 4 * base64编码字符串 5 * @param path 6 * 图片路径-具体到文件 7 * @return 8 */ 9 public static boolean generateImage(String imgStr, String path) { 10 if (StringUtils.isBlank(imgStr)) { 11 return false; 12 } 13 BASE64Decoder decoder = new BASE64Decoder(); 14 try { 15 // 解密 16 byte[] b = decoder.decodeBuffer(imgStr); 17 // 处理数据 18 for (int i = 0; i < b.length; ++i) { 19 if (b[i] < 0) { 20 b[i] += 256; 21 } 22 } 23 OutputStream out = new FileOutputStream(path); 24 out.write(b); 25 out.flush(); 26 out.close(); 27 return true; 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 return false; 32 } 33 34 /** 35 * @Description: 根据图片地址转换为base64编码字符串 36 * @return 37 */ 38 public static String getImageStr(String imgFile) { 39 InputStream inputStream = null; 40 byte[] data = null; 41 try { 42 inputStream = new FileInputStream(imgFile); 43 data = new byte[inputStream.available()]; 44 inputStream.read(data); 45 inputStream.close(); 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } 49 // 加密 50 BASE64Encoder encoder = new BASE64Encoder(); 51 return encoder.encode(data); 52 }
Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类。这个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。但是在Eclipse和MyEclipse中直接使用,却找不到该类。解决方法如下:
1.右键项目--》Build Path --》Configure Build Path
选择Libraries,点击JRE System Library,选择 Access rules,如果之前没有定义规则,会显示No rules defined
2. Access rules,点击Edit --》Add,然后点击Ok
3.在Resolution下拉列表框中选择Accessible,Rule Pattern 选择**,依次点击ok
/** * @Description:图片拼接 (注意:必须两张图片长宽一致哦) * @param files * 要拼接的文件列表 * @param type * 1横向拼接, 2 纵向拼接 */ public static boolean mergeImage(String[] files, int type, String targetFile) { int len = files.length; if (len < 1) { throw new RuntimeException("图片数量小于1"); } File[] src = new File[len]; BufferedImage[] images = new BufferedImage[len]; int[][] ImageArrays = new int[len][]; for (int i = 0; i < len; i++) { try { src[i] = new File(files[i]); images[i] = ImageIO.read(src[i]); } catch (Exception e) { throw new RuntimeException(e); } int width = images[i].getWidth(); int height = images[i].getHeight(); ImageArrays[i] = new int[width * height]; ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width); } int newHeight = 0; int newWidth = 0; for (int i = 0; i < images.length; i++) { // 横向 if (type == 1) { newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight(); newWidth += images[i].getWidth(); } else if (type == 2) {// 纵向 newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth(); newHeight += images[i].getHeight(); } } if (type == 1 && newWidth < 1) { return false; } if (type == 2 && newHeight < 1) { return false; } // 生成新图片 try { BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); int height_i = 0; int width_i = 0; for (int i = 0; i < images.length; i++) { if (type == 1) { ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0, images[i].getWidth()); width_i += images[i].getWidth(); } else if (type == 2) { ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth); height_i += images[i].getHeight(); } } // 输出想要的图片 ImageIO.write(ImageNew, targetFile.split("\.")[1], new File(targetFile)); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
/** * @param srcImgPath * 源图片路径 * @param tarImgPath * 保存的图片路径 * @param waterMarkContent * 水印内容 * @param markContentColor * 水印颜色 * @param font * 水印字体 */ public boolean addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent, Color markContentColor, Font font, int degree) { try { // 读取原图片信息 File srcImgFile = new File(srcImgPath);// 得到文件 Image srcImg = ImageIO.read(srcImgFile);// 文件转化为图片 int srcImgWidth = srcImg.getWidth(null);// 获取图片的宽 int srcImgHeight = srcImg.getHeight(null);// 获取图片的高 // 加水印 BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufImg.createGraphics(); g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null); g.setColor(markContentColor); // 根据图片的背景设置水印颜色 g.setFont(font); // 设置字体 // 设置水印的坐标 int x = srcImgWidth - 2 * getWatermarkLength(waterMarkContent, g); int y = srcImgHeight - 2 * getWatermarkLength(waterMarkContent, g); g.rotate(Math.toRadians(degree), (double) srcImgWidth / 2, (double) srcImgHeight / 2); float high = (float) (1648 / 3.7); for (int i = 0; i < 9; i++) { if (i % 2 == 0) { g.drawString(waterMarkContent, 0.0f - 500, i * high); // 画出水印 } else { g.drawString(waterMarkContent, 0.0f, i * high); // 画出水印 } } g.dispose(); // 输出图片 FileOutputStream outImgStream = new FileOutputStream(tarImgPath); ImageIO.write(bufImg, "jpg", outImgStream); System.out.println("添加水印完成"); outImgStream.flush(); outImgStream.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } public int getWatermarkLength(String waterMarkContent, Graphics2D g) { return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length()); } public static void main(String[] args) { Font font = new Font("微软雅黑", Font.BOLD, 150); // 水印字体 String srcImgPath = "d:/demo.jpg"; // 源图片地址 String tarImgPath = "d:/test1.jpg"; // 待存储的地址 String waterMarkContent = "本照片仅供行邮商品报关使用 本照片仅供行邮商品报关使用"; // 水印内容 Color color = new Color(0, 0, 0, 50); // 水印图片色彩以及透明度 addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color, font, 330); }