
正文
zxingjava代码 代码生成器 java
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 生成二维码后如何给该二维码添加信息
java可使用zxing生成二维码并为其添加信息。
以下是详细步骤:
1、创建MatrixToImageWriter类
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
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;
}
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);
}
}
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);
}
}
}
2、生成二维码并添加信息
import java.io.File;
import java.util.Hashtable;
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;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String text = "";
int width = 300;
int height = 300;
//二维码的图片格式
String format = "gif";
Hashtable hints = new Hashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
//生成二维码
File outputFile = new File("d:"+File.separator+"new.gif");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
}
}
相关问答
Q1: 怎么看JAVA开源项目的源码?
有个开源代码托管平台叫github来了解下。
GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git作为唯一的版本库格式进行托管,故名GitHub。
github也可以是一个远程代码仓库,你可以将你的代码或者项目上传到github仓库,这个完全没有问题,网上有github客户端管理软件,操作非常简单,就类似于:SVN、CVS。
github也是一个开源代码协作社区,通过github你可以参与别人的开源项目,也可以让别人参与你的开源项目。有些公司的产品,自己不想投入人力,但又不想放弃,就采用github代码托管的方式,将代码开源出去,让开发爱好者参与进来,其中docker就是一个很好的例子,也是开源最成功的一个项目。
下面介绍如何从github上拿到开源项目:zxin。
1、打开github官网“”。
2、根据“zxing”查找。
3、下载开源项目:zxing
操作:Cloneordowanload-DownloadZIP.
这样zxin源代码就拿到了,doc目录是项目文档,打开
index.html,全是英文,
要能读懂源代码,需要有很好的英文阅读能力,祝你好运。
Q2: java中zxing二维码怎么在扫描的时候跳转到指定页面
有没有没解决的?找到方法了,
@SuppressWarnings({"rawtypes", "unchecked"})
private static void createZxing() throws WriterException, IOException {
int width=300;
int hight=300;
String format="png";
String content="";//这里要注意!!!!!!!
HashMap hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION,
+ ErrorCorrectionLevel.M);//纠错等级L,M,Q,H
hints.put(EncodeHintType.MARGIN, 2); //边距
BitMatrix bitMatrix=new MultiFormatWriter().encode(content,
+ BarcodeFormat.QR_CODE, width, hight, hints);
File file=new File("D:/imag.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, file);
}
/*你需要跳转的页面前面加上http协议,扫描的软件比方说:微信,它会直接识别http
协议*/
关于zxingjava代码和代码生成器 java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







