import com.google.zxing.*;
import com.google.zxing.Reader;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import com.google.zxing.multi.MultipleBarcodeReader;
import javax.imageio.ImageIO;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.*;
/**
* 二維碼生成工具類
*
* @author KisChang
* @version 1.0
* @date 2015年12月03日
* @since 1.0
*/
public class ZXingUtils {
public static enum ImageType {
JPEG("jpeg"),PNG("png"),GIF("gif");
private String value;
ImageType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
/**編碼*/
public static class Encode {
private static Map<EncodeHintType, Object> HINTS;
static {
HINTS = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
HINTS.put(EncodeHintType.CHARACTER_SET, "UTF-8");
}
/**
* 生成二維碼
* @param widthAndHeight 高寬
* @param content 二維碼內容
* @param os 輸出流
*/
public static void buildQRCode(int widthAndHeight, String content, OutputStream os, ImageType imageType) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, HINTS);// 生成矩陣
MatrixToImageWriter.writeToStream(bitMatrix, imageType.getValue(), os);
}
public static void buildQRCode(String content, OutputStream os, ImageType imageType) throws WriterException, IOException {
buildQRCode(200, content, os, imageType);
}
/**
* 生成二維碼
* @param widthAndHeight 高寬
* @param content 二維碼內容
* @param filePath 輸出目錄
* @param fileName 輸出文件名
* @param imageType 輸出文件類型
*/
public static void buildQRCode(int widthAndHeight, String content, String filePath, String fileName, ImageType imageType) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, HINTS);
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToPath(bitMatrix, imageType.getValue(), path);// 輸出圖像
}
public static void buildQRCode(String content, String filePath, String fileName, ImageType imageType) throws WriterException, IOException {
buildQRCode(200, content,filePath,fileName,imageType);
}
}
/**解碼*/
public static class Decode {
private static final Map<DecodeHintType,Object> HINTS;
private static final Map<DecodeHintType,Object> HINTS_PURE;
static {
HINTS = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
HINTS.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
HINTS_PURE = new EnumMap<DecodeHintType,Object>(HINTS);
HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
}
/**
* 解析二維碼
*/
public static Collection<Result> readQRCode(File qrCode) throws ReaderException, IOException {
FileInputStream inputStream = new FileInputStream(qrCode);
return readQRCode(inputStream);
}
public static Collection<Result> readQRCode(InputStream inputStream) throws ReaderException, IOException {
LuminanceSource source = new BufferedImageLuminanceSource(ImageIO.read(inputStream));
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
Collection<Result> results = new ArrayList<Result>(1);
ReaderException savedException = null;
Reader reader = new MultiFormatReader();
try {
//尋找多個條碼
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = multiReader.decodeMultiple(binaryBitmap, HINTS);
if (theResults != null) {
results.addAll(Arrays.asList(theResults));
}
} catch (ReaderException re) {
savedException = re;
}
if (results.isEmpty()) {
try {
//尋找純條碼
Result theResult = reader.decode(binaryBitmap, HINTS_PURE);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
//尋找圖片中的正常條碼
Result theResult = reader.decode(binaryBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
//再次嘗試其他特殊處理
BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
Result theResult = reader.decode(hybridBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()){
throw savedException;
}else {
return results;
}
}
public static Result readQRCodeResult(File qrCode) throws ReaderException, IOException {
FileInputStream inputStream = new FileInputStream(qrCode);
return readQRCodeResult(inputStream);
}
public static Result readQRCodeResult(InputStream inputStream) throws ReaderException, IOException {
Collection<Result> results = readQRCode(inputStream);
if (!results.isEmpty()){
//尋找結果集中非空的結果
for (Result result : results){
if (result != null){
return result;
}
}
}
throw NotFoundException.getNotFoundInstance();
}
}
}
二維碼生成和解析代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.