图片转为base64和 图片的base64转为pdf的base64

  public  String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        String imgFile = imgPath;// 待处理的图片
        InputStream in = null;
        byte[] data = null;
        
  String encode = null; // 返回Base64编码过的字节数组字符串
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        try {
        // 读取图片字节数组
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            encode = encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
        return encode;
    }

//图片base64转pdfbase64,其中包括图片base64转换成图片,图片转换为pdf,pdf转换为base64编码
public String imageBase64ToPdfBase64(String bas64ImageStr){
        String pdfBase64 = "";//PDF的base64编码
        try{
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String time=sdf.format(date);
            //临时pdf文件路径  D:\app
            //图片名称流水号
            String custOrderNbr = time;

            //1.根据微厅返回的请求参数生成二维码 base64 编码
            bas64ImageStr = bas64ImageStr.replaceAll("\r|\n", "");

            //2.获取要存储文件的路径,即获取src资源文件编译后的路径(即classes路径)
            String url = this.getClass().getClassLoader().getResource("").getPath();
            //对路劲进行拼接添加
            String serviceImgPath = url + "serviceImg/";//服务器上存放二维码图片的路径
            String servicePdfPath = url + "servicePdf/";//服务器上存放二维码PDF的路径

            //3.判断服务器是否存在此文件夹,不存在则新建
            File file1 =new File(serviceImgPath);
            File file2 =new File(servicePdfPath);
            if(!file1.exists()  && !file1.isDirectory()) {
                file1.mkdir();
            }
            if(!file2.exists()  && !file2.isDirectory()) {
                file2.mkdir();
            }
            //4.二维码图片的文件名字和最终保存的二维码文件路径
            String fileImageName = custOrderNbr+"_legal.png";//二维码图片路径名字
            String filePdfName = custOrderNbr+"_legal.pdf";//PDF图片路径名字
            String lastImagePath = serviceImgPath+fileImageName;//最终二维码图片存放的路径
            String lastPdfPath = servicePdfPath+filePdfName;//最终二维码PDF存放的路径

            //5.首先保存二维码图片
            Base64ToImage(bas64ImageStr,lastImagePath);

            //6.然后把二维码图片转成PDF二维码文件进行储存
            ImgChangePDF(lastImagePath,lastPdfPath);

            //7.最后将PDF转成base64,PDF的base64才是最终能推送到签字版的
            File file3 =new File(lastPdfPath);
            pdfBase64 = PDFToBase64(file3);
            pdfBase64 = pdfBase64.replaceAll("\r|\n", "");

            //8.需要删除创建的临时文件
            File imagefile = new File(lastImagePath);
            if(imagefile.exists()){
                imagefile.delete();
            }
            File pdffile = new File(lastPdfPath);
            if(pdffile.exists()){
                pdffile.delete();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return pdfBase64;
    }
    //base64 转成图片
    public static boolean Base64ToImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 图像数据为空
            return false;
        // 解密
        try {
            // 解密
            Base64.Decoder decoder = Base64.getDecoder();
            // 去掉base64前缀 data:image/png;base64,
            imgStr = imgStr.substring(imgStr.indexOf(",", 1) + 1, imgStr.length());
            byte[] b = decoder.decode(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            // 保存图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            // 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 将图片转换成PDF
     * @param imageUrl        二维码图片路径 可以调用 FileUtil.getFileList() 方法
     * @param target        PDF的名字和位置
     */
    public static void ImgChangePDF(String imageUrl, String target) {
        //创建一个文档对象
        Document doc = new Document();
        try {
            //定义输出文件的位置
            PdfWriter.getInstance(doc, new FileOutputStream(target));
            //开启文档
            doc.open();

            //路径
            Image img = Image.getInstance(imageUrl);
            //获得宽高
            Float h = img.getHeight();
            Float w = img.getWidth();
            //统一压缩
            Integer percent = getPercent(h, w);
            //图片居中
            img.setAlignment(Image.MIDDLE);
            //百分比显示图
            img.scalePercent(percent);
            //设置高和宽的比例
            doc.add(img);

            // 关闭文档
            if(doc != null){
                doc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    /**
     * pdf 转64位编码
     * @param file
     * @return
     */
    public static String PDFToBase64(File file) {
        BASE64Encoder encoder = new BASE64Encoder();
        FileInputStream fin =null;
        BufferedInputStream bin =null;
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bout =null;
        try {
            fin = new FileInputStream(file);
            bin = new BufferedInputStream(fin);
            baos = new ByteArrayOutputStream();
            bout = new BufferedOutputStream(baos);
            byte[] buffer = new byte[1024];
            int len = bin.read(buffer);
            while(len != -1){
                bout.write(buffer, 0, len);
                len = bin.read(buffer);
            }
            //刷新此输出流并强制写出所有缓冲的输出字节
            bout.flush();
            byte[] bytes = baos.toByteArray();
            return encoder.encodeBuffer(bytes).trim();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fin.close();
                bin.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     * 压缩
     * @param
     */
    public static Integer getPercent(Float h,Float w)
    {
        Integer g=0;
        Float g2=0.0f;
        g2=480/w*100;
        g=Math.round(g2);
        return g;
    }
posted @ 2023-03-15 11:26  离地一厘米的飞翔  阅读(479)  评论(0)    收藏  举报