1. Mavenの依存性
<依存性> <のgroupId> com.google.zxing </のgroupId> <たartifactId>コア</たartifactId> <バージョン> 3.1.0 </バージョン> </依存> <依存性> <のgroupId>コモンズコーデック</のgroupId> <たartifactId>コモンズコーデック</たartifactId> <バージョン> 1.9 </バージョン> </依存> <依存性> <のgroupId> com.google.zxing </のgroupId> <たartifactId>するJavaSE </たartifactId> <バージョン> 3.2。 1 </バージョン> </依存関係>
2.二次元コード生成ツール
パッケージcom.eongb0.common.utils。 com.google.zxingインポート*。 輸入com.google.zxing.client.j2se.BufferedImageLuminanceSource。 輸入com.google.zxing.client.j2se.MatrixToImageWriter; 輸入com.google.zxing.common.BitMatrix; 輸入com.google.zxing.common.HybridBinarizer; 輸入com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 輸入javax.imageio.ImageIO; インポート持つjava.awt。*; 輸入java.awt.image.BufferedImage; インポートのjava.io.File; 輸入java.io.FileInputStream; インポートにjava.io.OutputStream; 輸入java.util.HashMapを; 輸入java.util.Map; //二维码工具类(使用ZXingjar包) パブリッククラスQRCodeUtils { //默认宽为300 整数は、幅= 600プライベート; 300の//デフォルトの高さ ;プライベート整数高さ= 600 //デフォルトの2次元コードの画像フォーマット プライベートimageFormat文字列=「PNG」; //デフォルトの文字は、2次元コードエンコード プライベート文字列charType =「UTF-8を「; フォールトトレランスの//デフォルトの二次元コードレベル //フォールトトレランスレベルL、M、Q、Lが最も低いH、Hが最も高い プライベートErrorCorrectionLevel corretionLevel = ErrorCorrectionLevel.M、 画像エッジを有する//二次元コード プライベート整数マージン= 0; //パラメータ次元コード 専用地図<EncodeHintType、オブジェクト> =新しい新しいencodeHitsのHashMap <EncodeHintType、オブジェクト>(); 公共QRCodeUtils(整数、整数の高さ、imageFormat文字列、文字列charType幅、 ErrorCorrectionLevel corretionLevel、整数マージン){ } } IF(幅= NULL){! this.width =幅; } IF(!高さ= NULL){ this.height =高; } IF(imageFormat = NULL!){ this.imageFormat = imageFormat。 } IF(charType = NULL!){ this.charType = charType。 } もし{(corretionLevel = NULL!) this.corretionLevel = corretionLevel。 } IF(!マージン= NULL){ this.margin =マージン。 公共QRCodeUtils(整数幅、整数の高さ、文字imageFormat、列charType、 ErrorCorrectionLevel corretionLevel){ この(幅、高さ、imageFormat、charType、corretionLevel、NULL); } 公共QRCodeUtils(整数幅、整数の高さ、文字imageFormat、列charType、整数マージン){ この(幅、高さ、imageFormat、charType、ヌル、マージン)。 } 公共QRCodeUtils(整数幅、整数の高さ、文字imageFormat、列charType){ この(幅、高さ、imageFormat、charType、NULL、NULL); } 公共QRCodeUtils(整数幅、整数の高さ、文字列imageFormat){ この(幅、高さ、imageFormat、NULL、NULL、NULL); } 公共QRCodeUtils(整数幅、整数高さ){ この(幅、高さ、NULL、NULL、NULL、NULL); } パブリックQRCodeUtils(){ } //は、二次元コードを初期化しますパラメータ プライベートボイドinitialParamers(){ コード//文字 encodeHits.put(EncodeHintType.CHARACTER_SET、this.charType)を、 フォールトトレランスL、Mの//レベル、Lが最も低いQ、H、Hが最も高い encodeHits.put(EncodeHintType.ERROR_CORRECTION 、this.corretionLevel); マージンと//二次元コード画像 ; encodeHits.put(EncodeHintType.MARGIN、マージン) } //取得した二次元コードの画像 公共のBufferedImage getBufferedImage(文字列コンテンツ){ initialParamers(); BufferedImageのBufferedImageの= NULL; {試みる )BitMatrix bitMatrix =新しいMultiFormatWriter(エンコード(コンテンツ、BarcodeFormat.QR_CODE、this.width、。 this.height、this.encodeHits)。 //去掉白边 INT [] REC = bitMatrix.getEnclosingRectangle()。 INT resWidth = REC [2] + 1。 INT resHeight = REC [3] + 1。 BitMatrix resMatrix =新しいBitMatrix(resWidth、resHeight)。 resMatrix.clear(); 以下のために(; I <resWidth、整数iが0 = I ++){ ため(INT J = 0; J <resHeight; J ++){ IF(bitMatrix.get(I + REC [0]、J + REC [1])){ resMatrix.set(i、j)は、 } } } // 2 INT幅= resMatrix.getWidth()。 INT高さ= resMatrix.getHeight()。 のBufferedImage =新しいBufferedImageの(幅、高さ、BufferedImage.TYPE_INT_ARGB)。 {(; X <幅X ++整数X = 0)のため のための(int型、Y = 0; Y <高さ; Y ++){ ?bufferedImage.setRGB(X、Y、resMatrix.get(X、Y)== trueの カラー。 BLACK.getRGB():Color.WHITE.getRGB()); } } }キャッチ(WriterException電子){ e.printStackTrace(); ヌルを返します。 } BufferedImageを返します。 } //将二维码保存到输出流中 公共ボイドwriteToStream(文字列の内容、のOutputStream OS){ initialParamers()。 {試します BitMatrixマトリックス=新しいMultiFormatWriter()エンコード(コンテンツ、BarcodeFormat.QR_CODE、this.width、this.height。 this.encodeHits)。 MatrixToImageWriter.writeToStream(マトリックス、this.imageFormat、OS)。 }キャッチ(例外e){ e.printStackTrace(); } } //将二维码图片保存为文件 公共ボイドcreateQrImage(文字列のコンテンツ、ファイルのファイル){ initialParamers()。 試み{ BitMatrixマトリックス=新しいMultiFormatWriter()エンコード(コンテンツ、BarcodeFormat.QR_CODE、this.width、this.height、this.encodeHits)。 MatrixToImageWriter.writeToFile(マトリックス、this.imageFormat、ファイル)。 }キャッチ(例外e){ e.printStackTrace(); } } //将二维码图片保存到指定路径 公共ボイドcreateQrImage(文字列のコンテンツ、文字列のパス){ initialParamers()。 {試みる )BitMatrixマトリックス=新しいMultiFormatWriter(エンコード(コンテンツ、BarcodeFormat.QR_CODE、this.width、this.height、this.encodeHits)。 MatrixToImageWriter.writeToPath(マトリックス、this.imageFormat、新しいファイル(パス).toPath()); // MatrixToImageWriter。 }キャッチ(例外e){ e.printStackTrace(); } } 公共ボイドnewcreateQrImage(文字列のコンテンツ、文字列のパス){ initialParamers()。 {試します BitMatrixマトリックス=新しいMultiFormatWriter()エンコード(コンテンツ、BarcodeFormat.QR_CODE、this.width、this.height、this.encodeHits)。 // MatrixToImageWriter.writeToPath(マトリックス、this.imageFormat、新しいファイル(パス).toPath()); //MatrixToImageWriter.w }キャッチ(例外e){ e.printStackTrace(); } } //识别图片二维码 パブリックストリングdecodeQrImage(ファイルファイル){ 文字列の内容= NULL; {試みる 値化値化=新しいHybridBinarizer(ソース)。 たBufferedImageのBufferedImage = ImageIO.read(新しいFileInputStreamを(ファイル)); LuminanceSourceソース=新しいBufferedImageLuminanceSource(BufferedImageの)。 BinaryBitmap画像=新しいBinaryBitmap(二値化)。 地図<DecodeHintType、オブジェクト> decodeHits =新しいHashMapの<DecodeHintType、オブジェクト>(); decodeHits.put(DecodeHintType.CHARACTER_SET、this.charType)。 結果をもたらす=新しいMultiFormatReader()デコード(画像、decodeHits)。 コンテンツ= result.getText()。 }キャッチ(例外e){ e.printStackTrace(); } コンテンツを返します。 } パブリック整数のgetWidth(){ 戻り幅。 } 公共ボイドにsetWidth(整数幅){ this.width =幅; } パブリック整数のgetHeight(){ リターンの高さ; } 公共ボイドsetHeightメソッド(整数の高さ){ this.height =高; } パブリック文字列getImageFormat(){ 戻りimageFormat。 } 公共ボイドsetImageFormat(文字列imageFormat){ this.imageFormat = imageFormat。 } パブリック文字列getCharType(){ 戻りcharType。 } 公共ボイドsetCharType(文字列charType){ this.charType = charType。 } 公共ErrorCorrectionLevel getCorretionLevel(){ corretionLevelを返します。 } 公共ボイドsetCorretionLevel(ErrorCorrectionLevel corretionLevel){ this.corretionLevel = corretionLevel。 } パブリック整数getMargin(){ 戻りマージン; } 公共ボイドsetMargin(整数マージン){ this.margin =マージン。 } パブリックマップ<EncodeHintType、OBJECT> GETHITS(){ 戻りencodeHits。 } 公共ボイドsetHits(MAP <EncodeHintType、OBJECT>ヒット){ this.encodeHits =ヒット。 } }
3.インタフェースFastDFSにアップロードし、得られた二次元コード
@ApiOperation( "二次元コードは、URLを取得") ("getCodeUrl")を@GetMapping 公共packageUrlForLink結果(@LoginUser(= isFull trueに)SYSUSERユーザー){ 文字列のリンク= ""; QRCodeUtils新しい新しいQRCodeUtils =(100,100)をQRコード、 QRコード。 setMargin(1); 文字列DEPTID = user.getDeptId(); SysDept DEPT = userService.findDeptById(DEPTID); 文字列の内容= "部署名:" + dept.getDeptName()+ " \ rを\ nは男を印刷:" +ユーザー.getUsername()+ "\ R \ nは印刷時間:" + GTime.getLogTime(); バッファ画像イメージqrCode.getBufferedImage =(コンテンツ); 試み{ //はFastDFSにアップロード画像をストリーミング: ByteArrayOutputStreamのOutputStream新しい新しいです= ByteArrayOutputStream(); ImageIO.write(画像、 "PNG"のOutputStream)。 InputStream InputStreamは=新しいれるByteArrayInputStream(outputStream.toByteArray());
//调用FastDFS中的接口将数据流保存到服务器返回图片地址 ストアのパスストアのパス= storageClient.uploadImageAndCrtThumbImage(のinputStream、inputStream.available()、 "PNG"、NULL); リンク= "のhttp://"。+ fileServerProperties.getFdfs()getWebUrl()+ "/" + storePath.getFullPath(); }キャッチ(IOExceptionを元){ ex.printStackTrace(); } マップの<string、オブジェクト>モデル=新規HashMapの<>(); model.put( "imgStr"、リンク)。 リターンResult.succeed(モデル); }
サムネイルのサイズを設定する4. FastDFS設定ファイル
FDFS: soTimeout:1500 のconnecttimeout:1000 トラッカー・リスト:IP:端口 親指-画像: 幅:100 高さ:100