包含qrcodejava的词条
本篇文章给大家谈谈qrcodejava,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java怎么生成带logo二维码
1、下载生成二维码所需要的jar包qrcode.jar;
2、直接上生成二维码的java代码
//需要导入的包
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;
/**
* 生成二维码(QRCode)图片
* @param content 二维码图片的内容
* @param imgPath 生成二维码图片完整的路径
* @param ccbpath 二维码图片中间的logo路径
*/
public static int createQRCode(String content, String imgPath,String ccbPath) {
try {
Qrcode qrcodeHandler = new Qrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
// System.out.println(content);
byte[] contentBytes = content.getBytes("gb2312");
//构造一个BufferedImage对象 设置宽、高
BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, 140, 140);
// 设定图像颜色 BLACK
gs.setColor(Color.BLACK);
// 设置偏移量 不设置可能导致解析出错
int pixoff = 2;
// 输出内容 二维码
if (contentBytes.length 0 contentBytes.length 120) {
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]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = "
+ contentBytes.length + " not in [ 0,120 ]. ");
return -1;
}
Image img = ImageIO.read(new File(ccbPath));//实例化一个Image对象。
gs.drawImage(img, 55, 55, 30, 30, null);
gs.dispose();
bufImg.flush();
// 生成二维码QRCode图片
File imgFile = new File(imgPath);
ImageIO.write(bufImg, "png", imgFile);
}catch (Exception e){
e.printStackTrace();
return -100;
}
return 0;
}
来自网友 孤独青鸟的博客
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);
}
}
java二维码扫码登录运用了什么技术
1. Web页面生成二维码
生成的二维码中必须要包含一个用于唯一标识用户的数据,这个唯一标识是为了确保将客户端(手机)与web网页绑定,避免其他人登录了你的账号。在这里可以生成以个随机的guid作为唯一标识。
生成二维码,大家可以使用jQuery qrcode插件。
2. 客户端扫描二维码
客户端在扫描二维码之前需要验证是否已经登录了账号,如果没有登录,则需要提示用户先登录。如果已经登录了,那么在扫描了二维码后应读取唯一标识guid并将sessionID一并发送给后台服务器(例如Java)。
3. 通过GoEasy服务器主动告知web网页登录成功
后台服务器接收到请求后通过GoEasy将sessionID主动推送给拥有相同唯一标识的web网页。
如何用java生成二维码
package common;
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.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.swetake.util.Qrcode;
/**
* 二维码生成工具类
* @author Cloud
* @data 2016-12-15
* QRCode
*/
public class QRCodeUtil {
//二维码颜色
private static final int BLACK = 0xFF000000;
//二维码颜色
private static final int WHITE = 0xFFFFFFFF;
/**
* span style="font-size:18px;font-weight:blod;"ZXing 方式生成二维码/span
* @param text a href="javascript:void();"二维码内容/a
* @param width 二维码宽
* @param height 二维码高
* @param outPutPath 二维码生成保存路径
* @param imageType 二维码生成格式
*/
public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType){
MapEncodeHintType, String his = new HashMapEncodeHintType, String();
//设置编码字符集
his.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
//1、生成二维码
BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);
//2、获取二维码宽高
int codeWidth = encode.getWidth();
int codeHeight = encode.getHeight();
//3、将二维码放入缓冲流
BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i codeWidth; i++) {
for (int j = 0; j codeHeight; j++) {
//4、循环将二维码内容定入图片
image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
}
}
File outPutImage = new File(outPutPath);
//如果图片不存在创建图片
if(!outPutImage.exists())
outPutImage.createNewFile();
//5、将二维码写入图片
ImageIO.write(image, imageType, outPutImage);
} catch (WriterException e) {
e.printStackTrace();
System.out.println("二维码生成失败");
} catch (IOException e) {
e.printStackTrace();
System.out.println("生成二维码图片失败");
}
}
/**
* span style="font-size:18px;font-weight:blod;"二维码解析/span
* @param analyzePath 二维码路径
* @return
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object zxingCodeAnalyze(String analyzePath) throws Exception{
MultiFormatReader formatReader = new MultiFormatReader();
Object result = null;
try {
File file = new File(analyzePath);
if (!file.exists())
{
return "二维码不存在";
}
BufferedImage image = ImageIO.read(file);
LuminanceSource source = new LuminanceSourceUtil(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
result = formatReader.decode(binaryBitmap, hints);
} catch (NotFoundException e) {
e.printStackTrace();
}
return result;
}
/**
* span style="font-size:18px;font-weight:blod;"QRCode 方式生成二维码/span
* @param content 二维码内容
* @param imgPath 二维码生成路径
* @param version 二维码版本
* @param isFlag 是否生成Logo图片 为NULL不生成
*/
public static void QRCodeCreate(String content, String imgPath, int version, String logoPath){
try {
Qrcode qrcodeHandler = new Qrcode();
//设置二维码排错率,可选L(7%) M(15%) Q(25%) H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
//N代表数字,A代表字符a-Z,B代表其他字符
qrcodeHandler.setQrcodeEncodeMode('B');
//版本1为21*21矩阵,版本每增1,二维码的两个边长都增4;所以版本7为45*45的矩阵;最高版本为是40,是177*177的矩阵
qrcodeHandler.setQrcodeVersion(version);
//根据版本计算尺寸
int imgSize = 67 + 12 * (version - 1) ;
byte[] contentBytes = content.getBytes("gb2312");
BufferedImage bufImg = new BufferedImage(imgSize , imgSize ,BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize , imgSize);
// 设定图像颜色 BLACK
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]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,130 ]. ");
}
/* 判断是否需要添加logo图片 */
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());
// 生成二维码QRCode图片
ImageIO.write(bufImg, imgType, imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* span style="font-size:18px;font-weight:blod;"QRCode二维码解析/span
* @param codePath 二维码路径
* @return 解析结果
*/
public static String QRCodeAnalyze(String codePath) {
File imageFile = new File(codePath);
BufferedImage bufImg = null;
String decodedData = null;
try {
if(!imageFile.exists())
return "二维码不存在";
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
decodedData = new String(decoder.decode(new ImageUtil(bufImg)), "gb2312");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return decodedData;
}
}
3、最后贴测试代码:
package test;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import common.ImageUtil;
import common.QRCodeUtil;
import jp.sourceforge.qrcode.QRCodeDecoder;
/**
* 二维码生成测试类
* @author Cloud
* @data 2016-11-21
* QRCodeTest
*/
public class QRCodeTest {
public static void main(String[] args) throws Exception {
/**
* QRcode 二维码生成测试
* QRCodeUtil.QRCodeCreate("", "E://qrcode.jpg", 15, "E://icon.png");
*/
/**
* QRcode 二维码解析测试
* String qrcodeAnalyze = QRCodeUtil.QRCodeAnalyze("E://qrcode.jpg");
*/
/**
* ZXingCode 二维码生成测试
* QRCodeUtil.zxingCodeCreate("", 300, 300, "E://zxingcode.jpg", "jpg");
*/
/**
* ZxingCode 二维码解析
* String zxingAnalyze = QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString();
*/
System.out.println("success");
}
}
关于qrcodejava和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-29,除非注明,否则均为
原创文章,转载请注明出处。