谁有二维码生成的源代码(java版本)的,能提供些吗?28
发布网友
发布时间:2023-10-10 09:53
我来回答
共2个回答
热心网友
时间:2024-10-20 12:49
可以考虑使用图形绘制来解决二维码的生成问题,使用实例如下:
生成二维码
package net.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.google.zxing.common.BitMatrix;
public final class MartixToImageWriter {
//私有不可更改的变量:生成二维码图片的颜色
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
//空的构造方法
public MartixToImageWriter() {
// TODO Auto-generated constructor stub
}
/**
* 静态方法
* BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,
* 通常用来做图片修改操作如大小变换、图片变灰、设置图片透明或不透明等。
* @param matrix 编码形式
* @return
*/
public static BufferedImage toBufferedImage(BitMatrix matrix)
{
//图片的宽度和高度
int width = matrix.getWidth();
int height = matrix.getHeight();
//BufferedImage.TYPE_INT_RGB将图片变为什么颜色
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(int x = 0;x < width;x++)
{
for(int y = 0;y < height;y++)
{
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 静态方法 用于生成图片
* @param matrix 编码形式
* @param format 图片类型
* @param file 文件(图片路径,图片名称)
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix,String format,File file) throws IOException
{
BufferedImage image = toBufferedImage(matrix);
if(!ImageIO.write(image, format, file))
{
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 输出
* @param matrix
* @param format
* @param stream
* @throws IOException
*/
public static void writeToStream(BitMatrix matrix,String format,OutputStream stream) throws IOException
{
BufferedImage image = toBufferedImage(matrix);
if(!ImageIO.write(image, format, stream))
{
throw new IOException("Could not write an image of format " + format);
}
}
}
测试二维码是否生成成功
package net.qrcode;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
public class TwoDimensionCode {
public static void main(String[] args) {
try
{
System.out.println("请输入您要生成二维码的信息");
Scanner input = new Scanner(System.in);
String content = input.next();
String path = "C:\\Users\\Administrator\\Desktop\\二维码图片库";
MultiFormatWriter multiFormatWrite = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 按照指定的宽度,高度和附加参数对字符串进行编码
//生成二维码
BitMatrix bitMatrix = multiFormatWrite.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
File file1 = new File(path,userId+".jpg");
// 写入文件
MartixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
System.out.println("二维码图片生成成功!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
热心网友
时间:2024-10-20 12:49
import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class QRCodeEncoderTest
{
/** Creates a new instance of QRCodeEncoderTest */
public QRCodeEncoderTest()
{
}
public static void create_image(String sms_info)throws Exception{
try{
qrcode testQrcode =new qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
String testString = sms_info;
byte[] d = testString.getBytes("gbk");
System.out.println(d.length);
//BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_INT_RGB);
BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 98, 98);
g.setColor(Color.BLACK);
// *最大字节数为120
if (d.length>0 && d.length <120){
boolean[][] s = testQrcode.calQrcode(d);
for (int i=0;i<s.length;i++){
for (int j=0;j<s.length;j++){
if (s[j][i]) {
g.fillRect(j*2+3,i*2+3,2,2);
}
}
}
}
g.dispose();
bi.flush();
File f = new File("D:\\QRCodeTest\\"+sms_info+".jpg");
if(!f.exists()){
f.createNewFile();
}
//创建图片
ImageIO.write(bi, "jpg", f);
} // end try
catch (Exception e) {
e.printStackTrace();
} // end catch
}
public static void main(String[] args) throws Exception {
System.out.println(new Date());
for(int i =1; i < 100000; i ++){
QRCodeEncoderTest.create_image(i+"");
}
System.out.println(new Date());
} // end main
}