-
背景:
因为目前业务需求,需要将存储的PNG图片BASE64编码转换成JPEG图片BASE64编码;
-
添加依赖
<dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency>
-
废话不多说,写了一个工具类,如下:
package com.insightzen.management.utils; import org.apache.axis.encoding.Base64; import sun.misc.BASE64Decoder; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; /** * @Author Mayz * @Date 2021/8/4 21:03 **/ public class PngToJpegUtils { // 测试 public static void main(String[] args) { //因为base64编码字符太长,就没有放 StringBuffer pic_base64 = new StringBuffer("png图片的base64编码"); System.out.println(pic_base64.toString()); PngToJpegUtils photo = new PngToJpegUtils(); String b = photo.changeBase64(pic_base64, "photo"); if (b != null){ System.out.println("成功!!!"); }else { System.out.println("失败!!!"); } } // 将base64转成图片 -------------(base64字符串,生成的图片名字) public String changeBase64(StringBuffer stringBuffer,String PictureName) { //保存路径 String rootpath = "C:\\image\\"; int xiegang = stringBuffer.indexOf("/"); int fenhao = stringBuffer.indexOf(";"); int douhao = stringBuffer.indexOf(","); String fileType = stringBuffer.substring(xiegang + 1, fenhao); StringBuffer data = stringBuffer.delete(0, douhao + 1); String filename = PictureName+"." + fileType; String savepath = rootpath + filename; System.out.println(savepath); BASE64Decoder base64Decoder = new BASE64Decoder(); String base64 = null; try { byte[] bytes = base64Decoder.decodeBuffer(data.toString()); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } //创建目录 createDir(rootpath); // 生成图片 OutputStream out = new FileOutputStream(savepath); out.write(bytes); out.flush(); out.close(); System.out.println("将base64转成图片保存的路径:"+savepath); InputStream is = new ByteArrayInputStream(bytes); BufferedImage bufferedImage = ImageIO.read(is); BufferedImage newBufferedImage = new BufferedImage( bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB); // TYPE_INT_RGB:创建一个RBG图像,24位深度,成功将32位图转化成24位 newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0,Color.WHITE, null); // write to jpeg file ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(newBufferedImage, "jpeg", outputStream); // ImageIO.write(newBufferedImage, "jpeg", new File("C:\\Users\\Eric\\Desktop\\image\\java.jpeg")); byte[] jpgfile =outputStream.toByteArray(); System.out.println("jpeg输出:"+jpgfile.toString()); base64 = Base64.encode(outputStream.toByteArray()); System.out.println(base64); // byte[] jpegs = wirteBytes(newBufferedImage, "jpeg"); // write to jpeg file base64 = Base64.encode(jpgfile); // System.out.println("base64: "+base64); outputStream.flush(); outputStream.close(); return base64; } catch (Exception e) { System.out.println("生成图片失败:PhotoUtils.changeBase64()"); } return null; } //String dirName = "D:/work/temp/temp0/temp1"; public static boolean createDir(String destDirName) { File dir = new File(destDirName); if (dir.exists()) { System.out.println("创建目录" + destDirName + "失败,目标目录已经存在"); return false; } if (!destDirName.endsWith(File.separator)) { destDirName = destDirName + File.separator; } //创建目录 if (dir.mkdirs()) { System.out.println("创建目录" + destDirName + "成功!"); return true; } else { System.out.println("创建目录" + destDirName + "失败!"); return false; } } /** * 将{@link BufferedImage}生成formatName指定格式的图像数据 * @param source * @param formatName 图像格式名,图像格式名错误则抛出异常 * @return */ public static byte[] wirteBytes(BufferedImage source,String formatName){ // Assert.notNull(source, "source"); // Assert.notEmpty(formatName, "formatName"); ByteArrayOutputStream output = new ByteArrayOutputStream(); Graphics2D g = null; try { for(BufferedImage s=source;!ImageIO.write(s, formatName, output);){ if(null!=g) throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName)); s = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB); g = s.createGraphics(); g.drawImage(source, 0, 0,null); } } catch (IOException e) { throw new RuntimeException(e); } finally { if (null != g) g.dispose(); } return output.toByteArray(); } //将base64转为字节流并字节流转为jpg形式 public InputStream changeBase642(StringBuffer stringBuffer,String PictureName) { int xiegang = stringBuffer.indexOf("/"); int fenhao = stringBuffer.indexOf(";"); int douhao = stringBuffer.indexOf(","); String fileType = stringBuffer.substring(xiegang + 1, fenhao); StringBuffer data = stringBuffer.delete(0, douhao + 1); String filename = PictureName+"." + fileType; BASE64Decoder base64Decoder = new BASE64Decoder(); try { byte[] bytes = base64Decoder.decodeBuffer(data.toString()); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } //字节数组转为输入流 InputStream stream = new ByteArrayInputStream(bytes); //png转为jpg BufferedImage bufferedImage = ImageIO.read(stream); // create a blank, RGB, same width and height, and a white background BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB); //TYPE_INT_RGB:创建一个RBG图像,24位深度,成功将32位图转化成24位 newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean flag = ImageIO.write(newBufferedImage, "jpg", out); bytes=out.toByteArray(); //将jpg字节流不为输入流 stream = new ByteArrayInputStream(bytes); return stream; } catch (Exception e) { System.out.println("图片转化失败:changeBase64()"); return null; } } }
-
大功告成!!!(如果有更简单的方法麻烦告知,嘻嘻_)
PNG图片BASE64编码转JPEG图片BASE64编码
最新推荐文章于 2024-05-31 09:05:13 发布