问题描述
publicclassCreateQRCodeImg{privatestaticfinalintBLACK=0xFF000000;privatestaticfinalintWHITE=0xFFFFFFFF;publicstaticBufferedImagetoBufferedImage(BitMatrixmatrix){intwidth=matrix.getWidth();intheight=matrix.getHeight();BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for(intx=0;x<width;x++){for(inty=0;y<height;y++){image.setRGB(x,y,matrix.get(x,y)?BLACK:WHITE);}}returnimage;}publicstaticvoidwriteToFile(BitMatrixmatrix,Stringformat,Filefile,StringlogoPath)throwsIOException{BufferedImageimage=toBufferedImage(matrix);Graphics2Dgs=image.createGraphics();//载入logoImageimg=ImageIO.read(newFile(logoPath));gs.drawImage(img,125,125,null);gs.dispose();img.flush();if(!ImageIO.write(image,format,file)){thrownewIOException("Couldnotwriteanimageofformat"+format+"to"+file);}}/***最终调用该方法生成二维码图片*@paramurl*@paramimgPath二维码生成的绝对路径*@paramlogoPath二维码中间logo绝对地址*@throwsException*/publicstaticvoidget2CodeImage(Stringurl,StringimgPath,StringlogoPath)throwsException{Stringformat="png";Hashtable<EncodeHintType,String>hints=newHashtable<EncodeHintType,String>();hints.put(EncodeHintType.CHARACTER_SET,"utf-8");BitMatrixbitMatrix=newMultiFormatWriter().encode(url,BarcodeFormat.QR_CODE,300,300,hints);FileoutputFile=newFile(imgPath);CreateQRCodeImg.writeToFile(bitMatrix,format,outputFile,logoPath);}}PS:zxing相关jar可到网上搜寻,或者在本人资源分享里面下载!
解决方案
解决方案二:
自己先做沙发,zxing的jar包分享地址http://download.csdn.net/detail/wendysunday123/7538779
解决方案三:
http://download.csdn.net/download/xmt1139057136/6944383这个事我写的例子,你看看吧,如果还有不懂,请加qq群:135430763共同学习!!!