자바 QR 코드 생 성 및 url 링크 부여 기능 구현

우선 pom 파일 에서 의존 도 를 가 져 옵 니 다.

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>
 
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>
도구 클래스

public class YmtUtil {
   public static byte[] getQRCodeImage(String text, int width, int height) throws 
        WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, 
        height);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        return pngData;
}
기능 실현
서비스 에 적 힌 코드 입 니 다.호출 하면 지정 한 저장 위치 에서 해당 하 는 QR 코드 를 찾 을 수 있 습 니 다.

//                192.168.0.21/erweima
String url = erweima;
 
//    ip  ,       ip    
InetAddress localhost = StrUtil.getLocalHostExactAddress();
 
//         
String URL= "http://localhost"+ url;
 
//      
qrcode = YmtUtil.getQRCodeImage(URL, 360, 360);
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
 
//             linux  :(/root/D:/opt/upFiles)  window  (D:/opt/upFiles)      linux  
File path = new File("/root/D:/opt/upFiles",       + ".jpg");
 
//          
ByteArrayInputStream inputStream = new ByteArrayInputStream(qrcode);
MockMultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
file.transferTo(path);
이상 은 자바 가 QR 코드 를 만 들 고 url 링크 를 부여 하 는 상세 한 내용 입 니 다.자바 가 QR 코드 를 만 드 는 것 에 관 한 자 료 는 다른 관련 글 을 주목 하 세 요!

좋은 웹페이지 즐겨찾기