首页 > 技术文章 > Java画图工具——Graphics2D

vettel0329 2019-10-18 11:28 原文

1.使用

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class TestGraphics2D {

    public static void main(String[] args) throws Exception {
        //从本地读取图片
        String path = "E://tmp/timg.jpg";
        BufferedImage backImage = ImageIO.read(new File(path));
        //生产画板
        Graphics2D graphics = backImage.createGraphics();
        //抗锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//去文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//去图片锯齿

        //生成并绘制二维码
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        createQR("http://www.baidu.com", pngOutputStream);
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(pngOutputStream.toByteArray()));
        graphics.drawImage(image, 50, 50, 100, 100, null);

        //读取url图片并绘制
        String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571378016581&di=31ba635c011b8a7179fb19da7f7a2bd3&imgtype=0&src=http%3A%2F%2Fis4.mzstatic.com%2Fimage%2Fpf%2Fus%2Fr30%2FPurple7%2Fv4%2Ff2%2F73%2F0c%2Ff2730c1a-e58f-084a-191a-90e3586f024a%2Fmzl.lngjyatg.png";
        BufferedImage drawImage = ImageIO.read(new URL(url));
        graphics.drawImage(drawImage,50,200,100,100, null);

        //绘制文字
        String ttfPath = "E://tmp/Francis.ttf";
        Font font = Font.createFont(Font.TRUETYPE_FONT, new File(ttfPath));
        font = font.deriveFont(Font.PLAIN,34);
//        Font font = new Font("楷体", Font.BOLD, 34);
        graphics.setColor(Color.BLACK);
        graphics.setFont(font);
        graphics.drawString("Hello World !", 200, 200);

        //保存图片到本地
        String resultPath = "E://tmp/back.jpg";
        ImageIO.write(backImage, "PNG", new File(resultPath));
    }

    //生成二维码
    public static void createQR(String codeContent, OutputStream out) throws Exception {
        //二维码参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置字符编码类型
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//设置纠错等级L/M/Q/H,纠错等级越高越不易识别
        hints.put(EncodeHintType.MARGIN, 1);//设置二维码边距,单位像素,值越小二维码距离四周越近
        //生成二维码,写入输出流
        BitMatrix bitMatrix = new MultiFormatWriter().encode(codeContent, BarcodeFormat.QR_CODE, 150, 150, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", out);
    }

}

 

2.文字居中

        //文字居中
        String outMsg = "This is center !";
        FontMetrics fontMetrics = graphics.getFontMetrics(font);
        int textWidth = fontMetrics.stringWidth(outMsg);// 文字宽度
        int centerX = backImage.getWidth() / 2;// 计算出中心点 x 位置
        graphics.drawString(outMsg, centerX - textWidth / 2, 400);

 

3.注意

  a.原点统一在画板左上角,绘制的图片和文字都位于第四象限。

  b.文字y轴方向距离为上边线到字体基线(baseline)的距离。

 

推荐阅读