java通过zxing生成二维码
2017-07-07 10:05
363 查看
需要的jar包
Zxing-javase-3.2.0.jar
zxing.jar
如出现类找不到请切换jdk版本
public class Test5 {
/**
* 生成二维码
*/
public static void createCode() {
//内容(可以写上网址例如:http://www.baidu.com)
String text = "大家好,我叫***";
//二维码的长宽
int width = 300;
int height = 300;
// 二维码的图片格式
String format = "png";
/**
* 设置二维码的参数
*/
HashMap hints = new HashMap();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 生成二维码文件(路径)
Path file = new File("D:/TDC-test.png").toPath();
//写入到文件
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析二维码
* @param filePath 二维码图片路径
* @return
*/
public static String decodeQr(String filePath) {
String retStr = "";
String retStr2="" ;
if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
return "图片路径为空!";
}
try {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(
filePath));
LuminanceSource source = new BufferedImageLuminanceSource(
bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap();
hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(bitmap,
hintTypeObjectHashMap);
retStr = result.getText();
retStr2=result.getBarcodeFormat().toString();
} catch (Exception e) {
e.printStackTrace();
}
return retStr+retStr2;
}
//测试
public static void main(String[] args) {
// TODO Auto-generated method stub
//调用生成二维码的方法
Test5.createCode();
//调用解析二维码的方法
System.out.println(decodeQr("D:/TDC-test.png"));
}
}
Zxing-javase-3.2.0.jar
zxing.jar
如出现类找不到请切换jdk版本
public class Test5 {
/**
* 生成二维码
*/
public static void createCode() {
//内容(可以写上网址例如:http://www.baidu.com)
String text = "大家好,我叫***";
//二维码的长宽
int width = 300;
int height = 300;
// 二维码的图片格式
String format = "png";
/**
* 设置二维码的参数
*/
HashMap hints = new HashMap();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 生成二维码文件(路径)
Path file = new File("D:/TDC-test.png").toPath();
//写入到文件
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析二维码
* @param filePath 二维码图片路径
* @return
*/
public static String decodeQr(String filePath) {
String retStr = "";
String retStr2="" ;
if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
return "图片路径为空!";
}
try {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(
filePath));
LuminanceSource source = new BufferedImageLuminanceSource(
bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap();
hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(bitmap,
hintTypeObjectHashMap);
retStr = result.getText();
retStr2=result.getBarcodeFormat().toString();
} catch (Exception e) {
e.printStackTrace();
}
return retStr+retStr2;
}
//测试
public static void main(String[] args) {
// TODO Auto-generated method stub
//调用生成二维码的方法
Test5.createCode();
//调用解析二维码的方法
System.out.println(decodeQr("D:/TDC-test.png"));
}
}
相关文章推荐
- java 通过zxing生成二维码
- java的二维码的生成与解析 通过zxing实现 火推
- Java通过zxing方式实现二维码的生成与解析
- Java通过Zxing生成二维码
- Java利用Zxing生成二维码
- zxing二维码的生成与解码(java)
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- 使用zxing生成嵌图片的二维码【java】
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- java zxing 生成带图片的二维码
- java使用zxing生成二维码
- java 使用zxing生成二维码
- Java利用Zxing生成二维码
- java 使用zxing 生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- java基于谷歌开发的zxing包开发的二维码生成工具