JAVA代码实现二维码制作
首先导入需要用到的jar包
(本人制作小工具包)可在文中上方免费下载
编写生成二维码格式编码
package common;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.swetake.util.Qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
public class QRCodeUtil
{
private static final int BLACK = -16777216;
private static final int WHITE = -1;
public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType)
{
Map
his.put(EncodeHintType.CHARACTER_SET, "utf-8");
try
{
BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);
int codeWidth = encode.getWidth();
int codeHeight = encode.getHeight();
BufferedImage image = new BufferedImage(codeWidth, codeHeight, 1);
for (int i = 0; i < codeWidth; i++) {
for (int j = 0; j < codeHeight; j++) {
image.setRGB(i, j, encode.get(i, j) ? -16777216 : -1);
}
}
File outPutImage = new File(outPutPath);
if (!outPutImage.exists()) {
outPutImage.createNewFile();
}
ImageIO.write(image, imageType, outPutImage);
}
catch (WriterException e)
{
e.printStackTrace();
System.out.println("二维码生成失败");
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("生成二维码图片失败");
}
}
public static void QRCodeCreate(String content, String imgPath, int version, String logoPath)
{
try
{
Qrcode qrcodeHandler = new Qrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(version);
int imgSize = 67 + 12 * (version - 1);
byte[] contentBytes = content.getBytes("gb2312");
BufferedImage bufImg = new BufferedImage(imgSize, imgSize, 1);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
gs.setColor(Color.BLACK);
int pixoff = 2;
if ((contentBytes.length > 0) && (contentBytes.length < 130))
{
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i] != 0) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
else
{
System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,130 ]. ");
}
if (logoPath != null)
{
File icon = new File(logoPath);
if (icon.exists())
{
int width_4 = imgSize / 4;
int width_8 = width_4 / 2;
int height_4 = imgSize / 4;
int height_8 = height_4 / 2;
Image img = ImageIO.read(icon);
gs.drawImage(img, width_4 + width_8, height_4 + height_8, width_4, height_4, null);
gs.dispose();
bufImg.flush();
}
else
{
System.out.println("Error: login图片不存在!");
}
}
gs.dispose();
bufImg.flush();
File imgFile = new File(imgPath);
if (!imgFile.exists()) {
imgFile.createNewFile();
}
String imgType = imgPath.substring(imgPath.lastIndexOf(".") + 1, imgPath.length());
ImageIO.write(bufImg, imgType, imgFile);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
