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 hints; static { hints = new enummap(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 hints; private static final map hints_pure; static { hints = new enummap(decodehinttype.class); hints.put(decodehinttype.try_harder, boolean.true); hints.put(decodehinttype.possible_formats, enumset.allof(barcodeformat.class)); hints_pure = new enummap(hints); hints_pure.put(decodehinttype.pure_barcode, boolean.true); } /** * 解析二维码 */ public static collection readqrcode(file qrcode) throws readerexception, ioexception { fileinputstream inputstream = new fileinputstream(qrcode); return readqrcode(inputstream); } public static collection readqrcode(inputstream inputstream) throws readerexception, ioexception { luminancesource source = new bufferedimageluminancesource(imageio.read(inputstream)); binarybitmap binarybitmap = new binarybitmap(new hybridbinarizer(source)); collection results = new arraylist(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 results = readqrcode(inputstream); if (!results.isempty()){ //寻找结果集中非空的结果 for (result result : results){ if (result != null){ return result; } } } throw notfoundexception.getnotfoundinstance(); } }}