慕粉1613346707
qq_慕姐3043116
一个怀有梦想的小菜鸟
搞定了,转成 byte[] 就能对上了。参考链接:https://blog.csdn.net/qq_27721169/article/details/96312517
码工1023
要我说,干脆赶紧放弃那种手动画图生成二维码的方式,直接使用谷歌的zxing类库,多香,何必自己折腾,毫无意义
归偃
本人知道了
慕少1969539
你把版本号的单引号去了试试
慕少1969539
你生成的二维码估计是UTF-8,生成二维码的编码格式得和解码的编码格式一致
慕村0971399
System.out.println("result");就是一个字符串是不变的,result是一个变量 宝贝长点心吧
薄荷味柠檬草
Mr_Jmx

资料下载里面有
凌啊凌啊
生成的就是png图片啊。你想要啥效果?
水月残阳下的梦7
超凡吖
我有一个综合版,不知道好不好用,你要的话可以发你邮箱。
快帮我拿点纸
代码有问题,你看看二维码设置的文本内容和一些参数
慕姐1376697
如果是本地的情况下,先百度如何读取本地数据,然后把读取的数据放进qrData,不就行了吗
慕粉3726127
应该没有关系的
Andy0726
慕粉0001083093
//注意二维码字节数限制,字节数太多生成的二维码解析会报错
输入中文时 :(与文件编码一致否则进行转编码 new String(str.getBytes(),"utf-8");)
Map hints = new HashMap<>()
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
解析时:
HashMap hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
慕虎5444613
貌似是没有读取到数据
大佑1989
是偏移量没加报错。加了偏移量就好了。
慕仰3266417
文件名字打错了没有
tansuofaxian
谢谢!
苾苼琐龠
writetofile()上那根线的意思是方法已经被弃用了吧,可能是版本问题
苏晓_爱菲
应该是二维码生成时的循环,没有加偏移量(视频中老师没有操作),导致解析时数组下标越界,图片中红线处,复制过来的源码是没有加的

慕粉4189078
二维码图案不能修改,没有有效期但是如果二维码的内容是一个网址,网址打开的页面包含的内容就可以修改,也有有效期
hang小白
//定义二维码参数
HashMap hints = new HashMap();
//字符集
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
慕雪8357630
慕粉1523472586
package com.wang.test;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
* 生成二维码
* @author Developer Name
*
*/
public class Test3 {
public static void main(String[] args) throws Exception {
String content = "ART M20327,#115664742,9TXZFHPB00185";
String outPath = "D:\\qrCode\\code.jpg";
String logoPath = "D:\\qrCode\\logo.png";
creatQrCode(1, 0, content, outPath, logoPath);
}
/**
* 生成二维码
* @param version 版本
* @param content 内容
* @param outPath 输出路径
* @param logoPath 图标路径
* @param pixoff 偏移量
* @throws Exception
*/
public static void creatQrCode(int version, int pixoff, String content, String outPath,
String logoPath) throws Exception {
// 图片尺寸
int width = 67 + 12 * (version - 1);
int height = 67 + 12 * (version - 1);
//int width = 30;
//int height = 30;
// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
Qrcode code = new Qrcode();
code.setQrcodeErrorCorrect('M');
code.setQrcodeEncodeMode('B');
// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
code.setQrcodeVersion(version);
BufferedImage bufferImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufferImage.createGraphics();
// 设置背景颜色
gs.setBackground(Color.WHITE);
// 设定图像颜色> BLACK
gs.setColor(Color.BLACK);
//清空画板
gs.clearRect(0, 0, width, height);
// 获得内容的字节数组,设置编码格式
byte[] contentBytes = content.getBytes("gb2312");
// 输出内容> 二维码
if (contentBytes.length > 0 && contentBytes.length < 120) {
boolean[][] codeOut = code.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
}
createPhotoAtCenter(bufferImage, logoPath);
gs.dispose();
bufferImage.flush();
ImageIO.write(bufferImage, "jpg", new File(outPath));
}
/**
* 加入logo
*
* @param bufImg 要添加的二维码对象
* @param logoPath 要添加logo的路径
* @return
* @throws Exception
*/
private static BufferedImage createPhotoAtCenter(BufferedImage bufImg, String logoPath) throws Exception {
Image image = ImageIO.read(new File(logoPath));
Graphics2D graphics = bufImg.createGraphics();
// 获取bufImg的中间位置
int centerX = bufImg.getMinX() + bufImg.getWidth() / 2 - 30 / 2;
int centerY = bufImg.getMinY() + bufImg.getHeight() / 2 - 30 / 2;
graphics.drawImage(image, centerX + 35, centerY, 30, 30, null);
graphics.dispose();
bufImg.flush();
return bufImg;
}
}
importtao
应该是忘了定义偏移量
qq_柒分醉_03927370
这个需要用try/catch处理异常的