java后台连接打印机打印2D画图

本文介绍如何在Java后台进行2D图形绘制,并结合控制器层操作,利用工具类实现二维码生成,最终实现打印机打印功能。
摘要由CSDN通过智能技术生成

首先加入工具类test用于画图使用

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Test {
   
    public static BufferedImage graphicsGenerationlovely(BufferedImage imageQR, String lovelyNick, int fansCount, String lovelyScore) {
   
        //整体图的高度和宽度
        int lovelyImageWidth = 400;
        int lovelyImageHeight = 531;
        //整体图合成
        BufferedImage bufferedImage = new BufferedImage(lovelyImageWidth, lovelyImageHeight, BufferedImage.TYPE_INT_RGB);
        //设置图片的背景色
        Graphics2D main = bufferedImage.createGraphics();
        main.fillRect(0, 0, lovelyImageWidth, lovelyImageHeight);
        //宝宝名称整合
        Graphics2D textIntro = bufferedImage.createGraphics();
        //颜色
        textIntro.setColor(new Color(51, 51, 51));
        //设置字体
        Font introFont = new Font("黑体", Font.PLAIN, 30);
        textIntro.setFont(introFont);
        FontMetrics fm = textIntro.getFontMetrics(introFont);
        int nickWidth = fm.stringWidth(lovelyNick);
        //因为名称为不固定,所以设置据左位置
        int nickWidthX = (lovelyImageWidth - nickWidth) / 2 - 10;
        //字体失真处理
        textIntro.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        textIntro.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        textIntro.drawString(lovelyNick, nickWidthX, 50);
        //外箱名称和外箱数量
        Graphics2D textIntro1 = bufferedImage.createGraphics();
        textIntro1.setColor(new Color(153, 153, 153));
        Font intro1Font = new Font("微软雅黑", Font.PLAIN, 26);
        textIntro1.setFont(intro1Font);
//        String fansAndScore = "名称:" + lovelyScore+"|" + "数量:" + fansCount + "件";
        String fansAndScore = "名称:" + lovelyScore;
        String fansAndScore2 = "数量:" + fansCount + "件";
        int infoWidth = fm.stringWidth(fansAndScore);
        int infoWidth2 = fm.stringWidth(fansAndScore2);

        int infoWidthX = (lovelyImageWidth - infoWidth) / 2;
        int infoWidthX2 = (lovelyImageWidth - infoWidth2) / 2;
        //失真处理
        textIntro1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        textIntro1.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        textIntro1.drawString(fansAndScore, infoWidthX, 100);
        textIntro1.drawString(fansAndScore2, infoWidthX2, 135);
        //可爱宝宝图
        Graphics graphicsQR = bufferedImage.getGraphics();
        graphicsQR.drawImage(imageQR, 70, 150, 250, 250, null);
        //固定文字
        Graphics2D fixed = bufferedImage.createGraphics();
        fixed.setColor(new Color(51, 51, 51));
        Font fixedFont = new Font("黑体", Font.PLAIN, 26);
        fixed.setFont(fixedFont);
        //失真处理
        fixed.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        fixed.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        fixed.drawString("扫描二维码获取详情", 70, 500);
        return bufferedImage;
    }
	//下面可以忽略,测试用的
    public static void main(String[] args) throws IOException {
   
        //需要添加的图片
        String imageQRUrl = "C:\\pdf\\20200616_145416_19614.png";
        //保存图片的路径
        String result = "C:\\pdf\\20200616_145416_1961400.png";
        FileInputStream inputStream = new FileInputStream(imageQRUrl);
        BufferedImage imageQR = ImageIO.read(inputStream);
        String lovelyNick = "外箱包装";
        int fansCount = 10;
        String socre = "4.9";
        BufferedImage bufferedImage = graphicsGenerationlovely(imageQR, lovelyNick, fansCount, socre);
        int temp = result.lastIndexOf(".") + 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值