programing

Android 응용 프로그램의 QR 코드를 생성하는 방법은 무엇입니까?

yoursource 2023. 1. 15. 13:18
반응형

Android 응용 프로그램의 QR 코드를 생성하는 방법은 무엇입니까?

안드로이드 앱에서 QR코드를 만들어야 하고 안드로이드 앱에서 QR코드를 만들 수 있는 라이브러리나 소스코드가 필요합니다.

필요한 라이브러리:

  1. (같은) 워터마크를 남기지 않다onbarcode라이브러리)
  2. 웹 서비스 API를 사용하여 QR코드를 생성하지 않음(Google의 라이브러리 zxing 등)
  3. 서드파티 설치 불필요 (QR Droid 등)

이미 iPhone(Objective-C)용으로 코드를 작성했습니다만, QR코드 생성기를 만들 때까지 Android용 빠른 수정이 필요합니다.첫 안드로이드 프로젝트이기 때문에 어떤 도움도 받을 수 있습니다.

zxing으로 QR을 만드는 내 코드입니다.

 QRCodeWriter writer = new QRCodeWriter();
    try {
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        ((ImageView) findViewById(R.id.img_result_qr)).setImageBitmap(bmp);

    } catch (WriterException e) {
        e.printStackTrace();
    }

ZXING에 대해 알아보셨나요?바코드를 만들기 위해 성공적으로 사용하고 있습니다.bitcoin 어플리케이션 src에서 전체 작업 예를 볼 수 있습니다.

// this is a small sample use of the QRCodeEncoder class from zxing
try {
    // generate a 150x150 QR code
    Bitmap bm = encodeAsBitmap(barcode_content, BarcodeFormat.QR_CODE, 150, 150);

    if(bm != null) {
        image_view.setImageBitmap(bm);
    }
} catch (WriterException e) { //eek }

어쩌면 이 오래된 주제일지도 모르지만 나는 이 도서관이 매우 유용하고 사용하기 쉽다는 것을 알았다.

QRGen

안드로이드에서 사용하는 예

 Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);

비트맵을 생성하기 위한 간단한 기능!ZXing1.3.jar만 사용!보정 레벨도 높음으로 설정했습니다!

PS: x와 y는 반대로 되어 있습니다.bit Matrix는 x와 y를 반대로 하기 때문입니다.이 코드는 정사각형 이미지에서 완벽하게 작동합니다.

public static Bitmap generateQrCode(String myCodeText) throws WriterException {
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage

    QRCodeWriter qrCodeWriter = new QRCodeWriter();

    int size = 256;

    ByteMatrix bitMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
    int width = bitMatrix.width();
    Bitmap bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < width; y++) {
            bmp.setPixel(y, x, bitMatrix.get(x, y)==0 ? Color.BLACK : Color.WHITE);
        }
    }
    return bmp;
}

편집

비트맵을 사용하는 것이 더 빠릅니다.비트맵 대신 픽셀 int 배열을 사용하는 setPixels(...)1개씩 setPixel:

        BitMatrix bitMatrix = writer.encode(inputValue, BarcodeFormat.QR_CODE, size, size);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = bitMatrix.get(x, y) ? BLACK : WHITE;
            }
        }

        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

저는 zxing-1.3 jar를 사용했고, 다른 답변에서 코드를 구현하기 위해 몇 가지 변경을 해야 했기 때문에 다른 사람에게 솔루션을 맡깁니다.다음을 수행했습니다.

1) zxing-1.3.jar를 찾아 다운로드하고 속성을 추가합니다(외부 jar 추가).

2) 액티비티 레이아웃에서 ImageView를 추가하고 이름을 붙입니다(이 예에서는 tnsd_iv_qr).

3) QR 이미지를 만들기 위한 코드를 액티비티에 포함합니다(이 예에서는 Bitcoin 결제를 위한 QR을 만들고 있었습니다).

    QRCodeWriter writer = new QRCodeWriter();
    ImageView tnsd_iv_qr = (ImageView)findViewById(R.id.tnsd_iv_qr);
    try {
        ByteMatrix bitMatrix = writer.encode("bitcoin:"+btc_acc_adress+"?amount="+amountBTC, BarcodeFormat.QR_CODE, 512, 512);
        int width = 512;
        int height = 512;
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (bitMatrix.get(x, y)==0)
                    bmp.setPixel(x, y, Color.BLACK);
                else
                    bmp.setPixel(x, y, Color.WHITE);
            }
        }
        tnsd_iv_qr.setImageBitmap(bmp);
    } catch (WriterException e) {
        //Log.e("QR ERROR", ""+e);

    }

궁금할 경우 변수 "btc_acc_address"는 문자열(BTC 주소 포함), 양 B입니다.TC는 물론 거래금액이 두 배입니다.

zxing은 웹 API를 제공하지 않습니다. 사실 구글이 API를 제공하는 것입니다. 이 API는 나중에 프로젝트에서 오픈 소싱된 소스 코드에서 제공됩니다.

Rob이 여기서 말한 것처럼 QR 코드 인코더용 Java 소스 코드를 사용하여 원시 바코드를 만든 다음 비트맵으로 렌더링할 수 있습니다.

더 쉬운 방법을 제안할 수 있습니다.바코드 스캐너를 의도별로 호출하여 바코드를 인코딩할 수 있습니다.몇 클래스가 합니다.android-integration주요 제품은 Intent Integrator입니다.그냥 전화하세요.shareText().

언급URL : https://stackoverflow.com/questions/8800919/how-to-generate-a-qr-code-for-an-android-application

반응형