/**
*
*/
package ceshi;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
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.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
/**
* @Filename: test.java
* @Version: 1.0
* @Author: 王超
* @Email:
*/
public class test {
//生成二维码
public static void shengcheng () throws WriterException, IOException{
String filePath= "D://";//生成图像的路径
String fileName = "zxing.png";
JSONObject json = new JSONObject();
json.put("zxing", "nihao");
json.put("author", "wangchao");
//内容
String content = json.toJSONString();
int width = 200;
int height=200;
String format = "png"; //类型
Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToFile(bitMatrix, format, new File(""+path));
System.out.println("输出成功");
}
public static void jiexi() throws IOException, NotFoundException{
String filePath = "D://zxing.png";
BufferedImage image;
image = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType,Object> hints = new HashMap<DecodeHintType,Object>();
hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
//对图像进行解析
Result result = new MultiFormatReader().decode(binaryBitmap,hints);
JSONObject context = JSONObject.parseObject(result.getText());
System.out.println("图片的内容是:");
System.out.println("author:" + context.getString("author"));
System.out.println("zxing:" + context.getString("zxing"));
System.out.println("图片的格式:");
System.out.println("encode" + result.getBarcodeFormat());
}
public static void main(String[] args) throws WriterException, IOException, NotFoundException{
jiexi();
}
*
*/
package ceshi;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
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.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
/**
* @Filename: test.java
* @Version: 1.0
* @Author: 王超
* @Email:
*/
public class test {
//生成二维码
public static void shengcheng () throws WriterException, IOException{
String filePath= "D://";//生成图像的路径
String fileName = "zxing.png";
JSONObject json = new JSONObject();
json.put("zxing", "nihao");
json.put("author", "wangchao");
//内容
String content = json.toJSONString();
int width = 200;
int height=200;
String format = "png"; //类型
Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToFile(bitMatrix, format, new File(""+path));
System.out.println("输出成功");
}
public static void jiexi() throws IOException, NotFoundException{
String filePath = "D://zxing.png";
BufferedImage image;
image = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType,Object> hints = new HashMap<DecodeHintType,Object>();
hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
//对图像进行解析
Result result = new MultiFormatReader().decode(binaryBitmap,hints);
JSONObject context = JSONObject.parseObject(result.getText());
System.out.println("图片的内容是:");
System.out.println("author:" + context.getString("author"));
System.out.println("zxing:" + context.getString("zxing"));
System.out.println("图片的格式:");
System.out.println("encode" + result.getBarcodeFormat());
}
public static void main(String[] args) throws WriterException, IOException, NotFoundException{
jiexi();
}
}
文章出处:http://kesun-shy.iteye.com/blog/2154169