1. 받는다는 의존
<의존성> <의 groupId> com.google.zxing </의 groupId> <artifactId를> 코어 </ artifactId를> <version>은 3.1.0 </ 버전> </ 의존성> <의존성> <의 groupId> 커먼스 코덱 </의 groupId> <artifactId를> 커먼스 코덱 </ artifactId를> <버전> 1.9 </ 버전> </ 의존성> <의존성> <의 groupId> com.google.zxing </의 groupId> <artifactId를> javase </ artifactId를> <version>은 3.2. 1 </ 버전> </ 의존성>
2. 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"; // 기본 문자가 이차원 코드 부호화 개인 문자열 charType = "UTF-8 "; 내결함성 // 기본 2 차원 코드 레벨 // 내결함성 레벨 L은, M은 Q가, L은 가장 낮은 H, H가 가장 높은 개인 ErrorCorrectionLevel corretionLevel = ErrorCorrectionLevel.M, 화상 가장자리 // 이차원 코드 전용 정수 마진 = 0; // 파라미터 차원 코드 프라이빗 맵 <EncodeHintType 개체> = 새로운 새 encodeHits HashMap의 <EncodeHintType 개체> (); 공공 QRCodeUtils (정수, 정수 높이 imageFormat 문자열, 문자열 charType 폭 ErrorCorrectionLevel corretionLevel 정수 마진) { } } IF (폭 = NULL) {! this.width = 폭; } 경우 (! 높이 = NULL) { this.height = 높이; } 경우 (imageFormat = NULL!) { this.imageFormat = imageFormat; } 경우 (charType = NULL!) { this.charType = charType; } 만약 {(corretionLevel = NULL!) this.corretionLevel = corretionLevel; } 경우 (마진 = 널!) { 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); } 공공 QRCodeUtils () { } // 2 차원 코드 파라미터 초기화 개인 무효 initialParamers () { // 인코딩 문자 encodeHits.put을 (EncodeHintType.CHARACTER_SET, this.charType) 내결함성 L, L은 가장 낮은 M, Q, H의 // 레벨은 H이다 최고 encodeHits.put (EncodeHintType.ERROR_CORRECTION, this.corretionLevel) 마진 // 2 차원 코드 화상 encodeHits.put (EncodeHintType.MARGIN 마진) } // 2 차원 코드 화상을 얻는 {) 공공 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 (); 경우 (나는 <resWidth; INT 나 0 = I ++) { 대 (INT J = 0; J <resHeight, J ++) { 경우 (bitMatrix.get (I + 촬영 [0], J + REC [1])) { resMatrix.set (I, J); } } } // 2 INT resMatrix.getWidth 폭 = (); INT 높이 = resMatrix.getHeight (); 의 BufferedImage = 새로운 BufferedImage의 (폭, 높이, BufferedImage.TYPE_INT_ARGB); 대 INT (X = 0, X <폭, X ++) { (INT에서 Y = 0, Y <높이, Y ++) {위한 bufferedImage.setRGB (X, Y, resMatrix.get (X, Y) == 사실? 컬러. BLACK.getRGB () : Color.WHITE.getRGB ()); } } } 캐치 (WriterException 전자) { e.printStackTrace (); NULL을 반환; } BufferedImage를 반환; } //将二维码保存到输出流中 공공 무효 writeToStream (문자열 콘텐츠의 OutputStream OS) { initialParamers를 (); {시도 BitMatrix 행렬 새로운 MultiFormatWriter = () 인 코드 (콘텐츠 BarcodeFormat.QR_CODE, this.width, this.height. this.encodeHits); MatrixToImageWriter.writeToStream (매트릭스 this.imageFormat, OS); } 캐치 (예외 전자) { e.printStackTrace (); } } //将二维码图片保存为文件 공공 무효 createQrImage (문자열 내용, 파일 파일) { initialParamers를 (); MatrixToImageWriter.writeToFile (매트릭스 this.imageFormat 파일); } 캐치 (예외 전자) { 은 try { BitMatrix 행렬 새로운 MultiFormatWriter = () 인 코드 (콘텐츠 BarcodeFormat.QR_CODE, this.width, this.height, this.encodeHits).; e.printStackTrace (); } } //将二维码图片保存到指定路径 공공 무효 createQrImage (문자열 내용, 문자열 경로) { initialParamers (); 시도 { BitMatrix 행렬 새로운 MultiFormatWriter = () 인 코드 (콘텐츠 BarcodeFormat.QR_CODE, this.width, this.height, this.encodeHits).; MatrixToImageWriter.writeToPath (매트릭스, this.imageFormat, 새로운 파일 (경로) .toPath ()); // MatrixToImageWriter. } 캐치 (예외 전자) { e.printStackTrace (); } } 공공 무효 newcreateQrImage (문자열 내용, 문자열 경로) { initialParamers (); {시도 BitMatrix 행렬 새로운 MultiFormatWriter = () 인 코드 (콘텐츠 BarcodeFormat.QR_CODE, this.width, this.height, this.encodeHits).; // MatrixToImageWriter.writeToPath (매트릭스 this.imageFormat 새로운 파일 (경로) .toPath ()); //MatrixToImageWriter.w } 캐치 (예외 전자) { 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.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, 개체> getHits () { 반환 encodeHits; } 공공 무효 setHits (지도 <EncodeHintType, 개체> 안타) { this.encodeHits = 히트; } }
3. 생성 된 2 차원 코드는 인터페이스 FastDFS 업로드
@ApiOperation ( "2 차원 코드는 URL 인수") @GetMapping ( "getCodeUrl")를 공개 packageUrlForLink 결과 (@LoginUser (= isFull true로) SYSUSER 사용자) { = ""문자열 링크, QRCodeUtils이 (100100) = 새로운 새로운 QRCodeUtils를 QR 코드; 하시면 자동을. setMargin (1); 문자열 deptId = user.getDeptId (); SysDept 부서 = userService.findDeptById (deptId) 문자열의 내용 = "부서 이름"+ dept.getDeptName () + " \ 연구 \ n 인쇄 사람 :"+ 사용자 .getUsername () + "\ 연구 \ n은 인쇄 시간 :"+ GTime.getLogTime (); BufferedImage의 이미지 qrCode.getBufferedImage = (콘텐츠) 은 try { //이 FastDFS에 업로드 할 이미지를 스트리밍 : 있는 ByteArrayOutputStream OutputStream에 새로운 새 =을 를 ByteArrayOutputStream (); ImageIO.write (화상 "PNG"의 OutputStream); 의 InputStream inputStream을 새로운이 InputStream을 = (outputStream.toByteArray ());
//调用FastDFS中的接口将数据流保存到服务器返回图片地址 StorePath storePath = storageClient.uploadImageAndCrtThumbImage (대한 InputStream inputStream.available () "PNG", NULL); 링크 = "HTTP : //". + fileServerProperties.getFdfs () getWebUrl () + "/"+ storePath.getFullPath (); } 캐치 (IOException이 예) { ex.printStackTrace (); } 지도 <문자열, 개체> 모델은 새의 HashMap을 <> () =; model.put ( "imgStr"링크); 반환 Result.succeed (모델); }
4. FastDFS는 썸네일의 크기를 설정 파일을 구성
fdfs : soTimeout : 1500 에 ConnectTimeout 1000 추적기리스트 : IP :端口 손가락 화상 : 폭 : 100 높이 : 100