JAVA_编辑图片功能_图片内嵌图片_BufferedImage转MultipartFile_保存本地_测试方法

 

 

    public static void main(String[] args) {
        /*
            // 网络图片地址     以下所使用的ImgUtil  是 Hutool 工具
            String reportTemplateUrlStr = "https://xxxxxxxxxxxxxx.com/xxxxxxxxxxxxxxxxx.jpg";
            URL reportTemplateUrl = new URL(reportTemplateUrlStr);
            BufferedImage image = ImgUtil.read(reportTemplateUrl);
         */

        //模板图片地址  本地图片地址
        File file = new File("C:\\Users\\admin\\Desktop\\doctor_card\\card_template.jpg");
        BufferedImage tem = ImgUtil.read(file);

        //嵌入图片地址  本地图片地址
        File fileInside = new File("C:\\Users\\admin\\Desktop\\doctor_card\\ricky.jpg");
        BufferedImage imgInside = ImgUtil.read(fileInside);
        //编辑嵌入图片的尺寸
        Image scaledInstance = imgInside.getScaledInstance(26, 26, Image.SCALE_DEFAULT);

        //创建画布
        BufferedImage image = new BufferedImage(tem.getWidth(), tem.getHeight(), BufferedImage.TYPE_INT_RGB);
        //获取画笔 将 模板图片画上画布
        image.getGraphics().drawImage(tem,0,0,null);

        //创建超级画笔
        Graphics2D g2d = image.createGraphics();

        //填充内嵌图片
        g2d.drawImage(scaledInstance,1446,639,null);

        //创建字体   姓名
        Font fontName = new Font("微软雅黑", Font.PLAIN, 50);
        //创建  颜色
        Color colorName = new Color(5,167,179);
        g2d.setFont(fontName);
        g2d.setColor(colorName);
        g2d.drawString("王二麻子",300,370);

        //抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, (RenderingHints.VALUE_TEXT_ANTIALIAS_GASP));
        //释放资源
        g2d.dispose();
        //BufferedImage 转 MultipartFile
        MultipartFile mfile = null;
        try {
            //BufferedImage 转化为 ByteArrayOutputStream
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", out);
            //ByteArrayOutputStream 转化为 byte[]
            byte[] imageByte = out.toByteArray();
            String base = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageByte);
            mfile = BASE64DecodedMultipartFile.base64ToMultipart(base);
        } catch (Exception e) {
            e.printStackTrace();
        }

        File toFile = null;
        if (mfile!=null && mfile.getSize()>0) {
            //获取文件原名称
            String originalFilename = mfile.getOriginalFilename();
            //获取文件格式
            String fileFormat = originalFilename.substring(originalFilename.lastIndexOf("."));

            //转file   生成随机文件名 (File.separator : 作用相当于 '\')
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            toFile = new File("C:\\Users\\admin\\Desktop"+File.separator+uuid+fileFormat);

            //最终绝对路径
            String absolutePath = null;

            try {
                //获取绝对路径  并去除[..]这样的符号,返回的是标准的绝对路径
                absolutePath = toFile.getCanonicalPath();

                //判断路径中的文件夹是否存在,如果不存在,则先创建文件夹
                String dirPath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));
                File dir = new File(dirPath);
                if (!dir.exists()){
                    dir.mkdirs();
                }

                InputStream ins = mfile.getInputStream();
                inputStreamToFile(ins,toFile);
                ins.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }




    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

posted @ 2022-08-02 10:36  zagwk  阅读(1592)  评论(0编辑  收藏  举报