package com.sicdt.sicsign.web.utils; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; public class MyImgUtil { /** * <br>描 述: 将图片BASE64字符串转为二进制数组 * <br>作 者: 七脉 * @param base64 new Image();img.src或canvas.toDataURL("image/png") * @return * @throws IOException */ public static byte[] imgBase64ToBytes(String base64) { sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); //因为参数base64来源不一样,需要将附件数据替换清空掉。如果此入参来自canvas.toDataURL("image/png"); base64 = base64.replaceAll("data:image/png;base64,", ""); //base64解码并转为二进制数组 byte[] bytes = null; try { bytes = decoder.decodeBuffer(base64); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } } catch (IOException e) { e.printStackTrace(); } return bytes; } /** * * <br>描 述: 图片/文件转二进制数组,这个方法有很多,只写一个 * <br>作 者: 七脉 * @param imgPath 图片路径 * @return * @throws FileNotFoundException */ public static byte[] imgToBytes(String imgPath) { File file = new File(imgPath); BufferedImage bi = null; ByteArrayOutputStream baos = null; try { //文件使用其他工具 bi = ImageIO.read(file); baos = new ByteArrayOutputStream(); int index = imgPath.lastIndexOf("."); String format = imgPath.substring(index+1); ImageIO.write(bi, format, baos); byte[] bytes = baos.toByteArray(); baos.close(); return bytes; } catch (IOException e) { e.printStackTrace(); } return null; } /** * * <br>描 述: 将二进制转换为图片 * <br>作 者: 七脉 * @param outPath 将图片输出到哪里 * @param savePath 保存位置 */ public static void bytesToImg(byte[] bytes,String savePath){ ByteArrayInputStream baos = new ByteArrayInputStream(bytes); try { BufferedImage bi = ImageIO.read(baos); File file = new File(savePath); ImageIO.write(bi, "png", file); baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * <br>描 述: 图片二进制数组转PDF二进制数组 * <br>作 者: 七脉 * @param imgBytes 图片二进制数组 * @return */ public static byte[] imgBytesToPdfBytes(byte[] imgBytes){ byte[] bytes = null; Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { PdfWriter.getInstance(document, baos); // 设置文档的大小 document.setPageSize(PageSize.A4); // 打开文档 document.open(); // 读取一个图片 Image image = Image.getInstance(imgBytes); float imageWidth = image.getScaledWidth(); int i = 0; while (imageWidth > 600) { image.scalePercent(100 - i); i++; imageWidth = image.getScaledWidth(); } image.setAlignment(Image.ALIGN_CENTER); // 插入一个图片 document.add(image); //转二进制数组 bytes = baos.toByteArray(); return bytes; } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null!=baos){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(null!=document){ document.close(); } } return bytes; } /** * * <br>描 述: 二进制转文件,什么样的二进制转什么样的文件 * <br>作 者: 七脉 * @param bytes 二进制数组 * @param savePath 文件保存路径 */ public static void byteArrayToFile(byte[] bytes,String savePath){ try { FileUtils.writeByteArrayToFile(new File(savePath), bytes); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { //bytesToImg(imgToByte("C://Users//Administrator//Desktop//test.png"),"C://Users//Administrator//Desktop//bytes2img.png"); byteArrayToFile(imgToByte("C://Users//Administrator//Desktop//test.png"),"C://Users//Administrator//Desktop//test2.png"); } }
时间: 2024-12-10 11:08:04