Java-很深我只知其一-BufferedImage对象绘制图片(P图)

BufferedImage对象绘制图片(P图)

  • maven依赖
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
</dependency>
  • 图片中加入简单的文字,并转换为Bsae64
        String result;
        try {
            BufferedImage buffImg = ImageIO.read(new File("图片绝对路径"));
            Graphics2D g = (Graphics2D)buffImg.getGraphics();//得到画笔对象

            //设置字体,大小
            g.setColor(Color.BLACK);
            g.setFont(new Font("黑体", Font.BOLD, 37));

            LocalDate now = LocalDate.now();
            g.drawString(String.valueOf(now.getYear()), 11, 11);
            g.drawString(String.valueOf(now.getMonthValue()), 111, 111);
            g.drawString(String.valueOf(now.getDayOfMonth()), 1111, 1111);
            ImageIO.write(buffImg, "PNG", new File("写出后的文件路径"));

            BASE64Encoder encoder = new BASE64Encoder();// 文件转码
            result = encoder.encode(getBytes(finalPath)).replaceAll("\r\n", "");

            g.dispose();
        } catch (Exception e) {
            throw new MyException("文件流转化出错");
        }
  •  旋转与缩放
/**
     *旋转与缩放
     * degree:旋转角度
     * shrinkW,shrinkH:缩放比例 原始值为1(值越大图片缩小)
     * moveX,moveY:图片在画布中的x,y位置
     * @param bufferedimage
     * @param degree
     * @return
     */
    private BufferedImage rotateImage(
            final BufferedImage bufferedimage,final int degree,
            final double shrinkW,final double shrinkH,
            final int moveX,final int moveY
    ) {
        int w = (int)Math.round(bufferedimage.getWidth()*shrinkW);
        int h =  (int)Math.round(bufferedimage.getHeight()*shrinkH);
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, type))
                .createGraphics()).setRenderingHint(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
        graphics2d.drawImage(bufferedimage, moveX,moveY, null);
        graphics2d.dispose();
        return img;
    }
  • 将文件对象转换为PDF
/**
     * 要加入PDF文件的全限定名
     * @param imgFilePath
     * 转换为PDF后文件的放置位置
     * @param pdfFilePath
     * //调节图片在PDF中的尺寸
     * @param ratio1
     * @param ratio2
     * @param ratio3
     * @param ratio4
     * 文件在PDF中的X,Y轴坐标
     * @param absoluteX
     * @param absoluteY
     * @throws Exception
     */
public void image2pdf(String imgFilePath, String pdfFilePath, Float ratio1, Float ratio2, Float ratio3, Float ratio4, Float absoluteX, Float absoluteY) throws Exception {
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        FileOutputStream fos = null;
        fos = new FileOutputStream(pdfFilePath);
        PdfWriter.getInstance(document, fos);
        //输出为A4纸张
        document.setPageSize(PageSize.A4);
        document.open();
        com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(imgFilePath);
        //调节图片在PDF中的尺寸
        float imageHeight = image.getScaledHeight() / ratio1 * ratio2;
        float imageWidth = image.getScaledWidth() / ratio3 * ratio4;
        image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER);
        // 设置图片的绝对位置
        image.setAbsolutePosition(absoluteX, absoluteY);
        image.scaleAbsolute(imageWidth, imageHeight);
        // 插入一个图片
        document.add(image);
        document.close();
        fos.flush();
        fos.close();
    }

 

/**
     * 获得指定文件的byte数组
     */
    private static byte[] getBytes(String filePath){
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
            byte[] b = new byte[8192];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值