Java畫圖工具——Graphics2D


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)的距離。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM