Java 画图并使文本居中,Graphics2D属性设置

package com.sinosoft.eflex.service.sms;

import com.thoughtworks.xstream.core.util.Base64Encoder;
import org.apache.commons.lang.RandomStringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

/**
 * @author Sonny
 * @create 2023/6/28
 * @desc
 */
public class DemoTest1 {

    public static void main(String[] args)throws Exception {

        int len = 4, width = 80, height = 30;
        String randomStr = null;
        //生成验证码文本内容
        String VERIFY_CODES = "3456789abcdefghjkmnpqrxtuvwxyABCDEFGHJKLMNPQRSTUVWXY";//取消0o1i2zZ等容易混淆的字符
        randomStr = RandomStringUtils.random(len, VERIFY_CODES);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //构建验证码图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D gra = image.createGraphics();
        //设置背景颜色
        gra.setColor(new Color(228, 230, 229));
        //设置画布
        gra.fillRect(0, 0, width, height);
        //设置文本颜色
        gra.setColor(Color.red);
        //设置文本字体
        Font font = new Font("Lucida Sans", Font.BOLD, 20);
        gra.setFont(font);
        //文本抗锯齿
        gra.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        gra.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //为画布添加文字,并居中
        FontMetrics fm = gra.getFontMetrics(font);
        int textWidth = fm.stringWidth(randomStr);
        int x = (width - textWidth) / 2;
        int ascent = fm.getAscent();
        int descent = fm.getDescent();
        int y = (height - (ascent + descent)) / 2 + ascent;
        gra.drawString(randomStr, x, y);
        //绘制干扰线
//        Random random = new Random();
//        gra.setColor(getRandColor(160, 200));// 设置线条的颜色
//        for (int i = 0; i < 1; i++) {
//            int x = random.nextInt(width - 1);
//            int y = random.nextInt(height - 1);
//            int xl = random.nextInt(6) + 1;
//            int yl = random.nextInt(12) + 1;
//            gra.drawLine(x, y, x + xl + 40, y + yl + 20);
//        }
        //转为二进制流
        Base64Encoder encoder = new Base64Encoder();
        ImageIO.write(image, "jpg", outputStream);
        String prefix = "data:image/jpg;base64,";
        String imgBase64 = prefix + encoder.encode(outputStream.toByteArray());
        System.out.println(imgBase64);

//        String path = "D:/00000/";
//        //png图
//        SpecCaptcha captcha = new SpecCaptcha(130, 40);
//        System.out.println("二进制:"+captcha.toBase64());
//        FileOutputStream outputStream = new FileOutputStream(path + "0png类型.png");
//        System.out.println(captcha.text());
//        captcha.out(outputStream);
//        //gif动图
//        GifCaptcha gifCaptcha = new GifCaptcha(130, 40);
//        FileOutputStream outputStream1 = new FileOutputStream(path + "1gif类型.gif");
//        //设置纯大写字母验证码TYPE_ONLY_UPPER
//        gifCaptcha.setCharType(Captcha.TYPE_ONLY_UPPER);
//        //设置字体
//        gifCaptcha.setFont(Captcha.FONT_5);
//        System.out.println(gifCaptcha.text());
//        gifCaptcha.out(outputStream1);
//        //中文类型
//        ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 40);
//        FileOutputStream outputStream2 = new FileOutputStream(path + "2中文类型.png");
//        System.out.println(chineseCaptcha.text()); chineseCaptcha.out(outputStream2);
//        //中文类型gif动图
//        ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 40);
//        FileOutputStream outputStream3 = new FileOutputStream(path + "3中文类型动图.gif");
//        System.out.println(chineseGifCaptcha.text()); chineseGifCaptcha.out(outputStream3);
//        //算术类型
//        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(180, 40);
//        FileOutputStream outputStream4 = new FileOutputStream(path + "4算术类型.png");
//        //设置几位数的运算,几个数计算
//        arithmeticCaptcha.setLen(3);
//        //打印算式
//        System.out.println(arithmeticCaptcha.getArithmeticString());
//        //打印算式的结果
//        System.out.println(arithmeticCaptcha.text());
//        arithmeticCaptcha.out(outputStream4);
    }
    
}

你可能感兴趣的:(开发技巧,java)