Java之二维码工具类


先上效果


文件结构


下面开始

引入jar包


<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>


    <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>

</dependency>


QRCodeUtils 工具类

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.itextpdf.text.pdf.qrcode.ErrorCorrectionLevel;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.UUID;

public class QRCodeUtils {
//    private static Integer WIDTH_PIX = 450;
//    private static Integer HEIGHT_PIX = 500;
    private static String TYPE = "JPEG";
    private static String CHAR_TYPE = "UTF-8";

    /**
     * 二维码填充颜色
     */
    //黑色
    private static Integer COLOR_BLACK = 0XFF000000;
    //白色
    private static Integer COLOR_WHITE = 0XFFFFFFFF;
    private static Integer COLOR_GREEN = 0xff00ff00;
    private static Integer COLOR_PINK = 0xFFCBC0FF;
    //二维码字体设置
    private static String FONT_NAME = "Consolas";
    //字体颜色
    private static Color FONT_COLOR = Color.GREEN;
    //字体大小
    private static Integer FONT_SIZE =12;

    /**
     * 写入二维码
     * @param text
     * @param filePath
     * @param logoPath
     * @param COLOR
     * @param width_pix
     * @param height_pix
     * @return
     * @throws Exception
     */
    public static File writeQRImg(String text,String filePath,String logoPath,Integer COLOR,Integer width_pix,Integer height_pix) throws Exception {
        //配置参数
        Hashtable<EncodeHintType,Object> hints= new Hashtable<EncodeHintType, Object>();
        //字符编码
        hints.put(EncodeHintType.CHARACTER_SET,CHAR_TYPE);
        //容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN,3);
        //生成二维码
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE,width_pix,height_pix,hints);
        //得到二维码的宽高
        int codeWidth = bitMatrix.getWidth();
        int codeHeight = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(codeWidth,codeHeight,BufferedImage.TYPE_INT_RGB);
        for (int i = 0;i<codeWidth;i++){
            for (int j= 0;j<codeHeight;j++){
                image.setRGB(i,j,bitMatrix.get(i,j)?COLOR:COLOR_WHITE);
            }
        }

        //写入logo
        writeLogo(image,logoPath,text);

        File outPutImage= getQRImgFile(filePath);
        //写入图片
        ImageIO.write(image,TYPE,outPutImage);
        return outPutImage;
    }

    /**
     * 写logo
     * @param image
     * @param logoPath
     * @param text
     * @throws Exception
     */
    private static void writeLogo(BufferedImage image, String logoPath,String text) throws Exception {
        if (logoPath!=null&&logoPath.length()>0) {
            File logoPic =new File(logoPath);
            if (logoPic.exists()) {
                BufferedImage logoBufferedImage=ImageIO.read(logoPic);
                int widthLogo = logoBufferedImage.getWidth(null)>image.getWidth()*2/10?
                        (image.getWidth()*2/10):logoBufferedImage.getWidth(null);
                int heightLogo= logoBufferedImage.getHeight(null)>image.getHeight()*2/10?
                        (image.getHeight()*2/10):logoBufferedImage.getHeight(null);

                //设定在图片中间
                int x=(image.getWidth()-widthLogo)/2;
                int y=(image.getHeight()-heightLogo)/2;
                //在原来基础上再加图片
                Graphics2D graphics2D = image.createGraphics();

                //绘制logo图片
                graphics2D.drawImage(logoBufferedImage,x,y,widthLogo,heightLogo,null);
                //绘制圆角矩形
                graphics2D.drawRoundRect(x,y,widthLogo,heightLogo,15,15);
                //设置边框宽度
                graphics2D.setStroke(new BasicStroke(2));
                //边框颜色
                graphics2D.setColor(Color.WHITE);
                graphics2D.drawRect(x,y,widthLogo,heightLogo);

                //设置字体大小
                Font font=new Font(FONT_NAME, Font.PLAIN,FONT_SIZE);

//                graphics2D.setColor(FONT_COLOR);
//                graphics2D.setFont(font);
//                Integer fontStartIndex=image.getWidth()/2-(int)(text.length()*FONT_SIZE*0.7)/2;
//                graphics2D.drawString(text,fontStartIndex,image.getHeight()-FONT_SIZE);

                //释放图像资源
                graphics2D.dispose();
                logoBufferedImage.flush();
                image.flush();
            }
        }

    }

    /**
     * 得到二维码图片
     * @param filePath
     * @return
     * @throws Exception
     */
    private static File getQRImgFile(String filePath) throws Exception {
//        String fileName= UUID.randomUUID().toString()+"."+TYPE;
        String fileName= "newPic"+"."+TYPE;
        File outPutImage = new File(filePath+File.separator+fileName);
        if (outPutImage.exists()) {
            outPutImage.delete();
        }
        outPutImage.createNewFile();
        return outPutImage;
    }

    /**
     * 写图片下方的水印
     * @param pressText
     * @param newImg
     * @param targetImg
     * @param fontStyle
     * @param color
     * @param fontSize
     * @param width
     * @param height
     * @throws Exception
     */
    public static void pressText(String pressText,String newImg,String targetImg,int fontStyle,
    Color color,int fontSize,int width,int height) throws Exception {
        //设置字体开始位置坐标


        File file=new File(targetImg);
        Image src=ImageIO.read(file);
        int imageW=src.getWidth(null);
        int imageH=src.getHeight(null);
        BufferedImage image =new BufferedImage(imageW,imageH,BufferedImage.TYPE_INT_RGB);

        Graphics graphics=image.createGraphics();
//        int startX=(width/2-graphics.getFontMetrics().stringWidth(pressText)/2);
//        int startY=height-(height-width)/2;
        int startX=image.getWidth()/2-(int)(pressText.length()*FONT_SIZE*0.7)/2;
        int startY=image.getHeight()-FONT_SIZE;
        graphics.drawImage(src,0,0,imageW,imageH,null);
        graphics.setColor(color);
        graphics.setFont(new Font(null,fontStyle,fontSize));//设置字体
        graphics.drawString(pressText,startX,startY);//写入文字
        graphics.dispose();

        FileOutputStream fileOutputStream= new FileOutputStream(newImg);
        ImageIO.write(image,"JPEG",fileOutputStream);
        //
        JPEGImageEncoder jpegImageEncoder= JPEGCodec.createJPEGEncoder(fileOutputStream);
        jpegImageEncoder.encode(image);
        fileOutputStream.close();
        System.out.println("image press success");
    }
}


读取文件工具类 (写不写无所谓 方便定义文字而已)

import java.io.*;

public class ReadFromFile {
    /**
     * 读取文件
     * @param src
     * @return
     */
    public static String readFromFile(File src){
        try {
            //汉字的字符集不匹配,比如说用UTF-8字符集去解析GBK字符集的汉字就会变成乱码
//            这里不要用FileReader,这个类用的是默认字符集去读取文本,改用InputStreamReader,它的构造方法中能够指定字符集,让它作为BufferedReader的源,就不会乱码了
//            如果还是乱码,就把GBK改成UTF-8
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(src),"GBK"));
            FileInputStream fileInputStream = new FileInputStream(src);
            BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream);
            StringBuilder stringBuilder = new StringBuilder();
            String content=null;
            while ((content=bufferedReader.readLine())!=null){
                stringBuilder.append(content);
                stringBuilder.append("\r\n");
            }

            return stringBuilder.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}


测试类 

import org.junit.Test;

import java.awt.*;
import java.io.File;

public class QRCodeUtilsTest {
    private static Integer COLOR_BLACK = 0XFF000000;
    //白色
    private static Integer COLOR_WHITE = 0XFFFFFFFF;
    private static Integer COLOR_GREEN = 0xff00ff00;
    private static Integer COLOR_PINK = 0xE86CA0;

    @Test
    public void testWriteQRImg() throws Exception {
        String outPath="D:/QRCodetest";
        String newImg="D:/QRCodetest/imageWithText.jpg";
        String targetImg="D:/QRCodetest/newPic.jpeg";
        int fontStyle=1;
        int fontSize=20;//字体大小
        int width=800;
        int height=800;
        File pressFile =new File("D:/QRCodetest/pressText.txt");
        String pressText =ReadFromFile.readFromFile(pressFile);
        //读取文件 转为字符串
        File file=new File("D:/QRCodetest/text.txt");
        String text=ReadFromFile.readFromFile(file);
        QRCodeUtils.writeQRImg(text,outPath,"D:/QRCodetest/logo.jpg",COLOR_PINK,width,height);
        QRCodeUtils.pressText(pressText,newImg,targetImg,fontStyle, Color.BLACK,fontSize,width,height);
    }
}

完毕




参考博客:

http://blog.csdn.net/yelllowcong/article/details/78005319

Java生成二维码工具类的源码是一段用Java语言编写的程序代码,用于生成二维码图片。下面是一个简单的Java生成二维码工具类的源码示例: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) { String text = "https://www.example.com"; // 要生成二维码的内容 int width = 300; // 二维码图片的宽度 int height = 300; // 二维码图片的高度 String format = "png"; // 二维码图片的格式 try { BufferedImage image = generateQRCode(text, width, height); saveImage(image, format, "qrcode.png"); System.out.println("二维码已生成。"); } catch (Exception e) { e.printStackTrace(); } } // 生成二维码图片 public static BufferedImage generateQRCode(String text, int width, int height) throws Exception { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); int matrixWidth = bitMatrix.getWidth(); BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, matrixWidth, matrixWidth); graphics.setColor(Color.BLACK); for (int i = 0; i < matrixWidth; i++) { for (int j = 0; j < matrixWidth; j++) { if (bitMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } return image; } // 保存二维码图片 public static void saveImage(BufferedImage image, String format, String filePath) throws IOException { ImageIO.write(image, format, new File(filePath)); } } ``` 这个工具类使用了Google的ZXing库来生成二维码。主要包含两个方法:`generateQRCode()`用于生成二维码图片,`saveImage()`用于保存二维码图片到文件。 使用时,只需要指定要生成二维码的内容、图片的宽度和高度,然后调用`generateQRCode()`方法获取生成的二维码图片,最后保存到文件即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值