Zxing qrcode QR 코드 사용(생 성 및 해석)

Zxing 을 사용 하여 QR 코드 를 생 성하 고 해석 합 니 다.예 를 들 어 Zxing-2.0 버 전 을 사용 합 니 다.사용 하기에 간단 합 니 다.벽돌 을 찍 는 것 을 환영 합 니 다.(퍼 가기
 
package com.whl.qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class CreateQrCodeTest {

	public static void createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat){
        try{
            // Create the ByteMatrix for the QR-Code that encodes the given String.
            Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
            
            // Make the BufferedImage that are to hold the QRCode
            int matrixWidth = byteMatrix.getWidth();
            BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
            image.createGraphics();
            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, matrixWidth, matrixWidth);

            // Paint and save the image using the ByteMatrix
            graphics.setColor(Color.BLACK);
            for (int i = 0; i < matrixWidth; i++){
                for (int j = 0; j < matrixWidth; j++){
                    if (byteMatrix.get(i, j)){
                        graphics.fillRect(i, j, 1, 1);
                    }
                }
            }

            ImageIO.write(image, imageFormat, outputStream);
        }catch (Exception ex) {
        	ex.printStackTrace();
        }
    }
	
	public static void readQrCode(InputStream inputStream) throws IOException{
		
        //get the data from the input stream
		BufferedImage image = ImageIO.read(inputStream);

		//convert the image to a binary bitmap source
		LuminanceSource source = new BufferedImageLuminanceSource(image);
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

		//decode the barcode
		QRCodeReader reader = new QRCodeReader();

		Result result = null ;
		try {
		 result = reader.decode(bitmap);
		} catch (ReaderException e) {
		 //the data is improperly formatted
			e.printStackTrace();
		}

		System.out.println(result.getText());
	}

	public static void main(String[] args) throws IOException {

//		createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"name:whl",300,"JPEG");
	
		readQrCode(new FileInputStream(new File("d:\\qrcode.jpg")));
	}

}

좋은 웹페이지 즐겨찾기