2017-05-03 6 views
2

QR 바코드로 앱을 만들고 있습니다. 바코드가 올바르게로드되지만 어떻게 든 탭을 클릭하거나 메뉴를 클릭하면 약 3 ~ 5 초 후에 느리게 비트가로드됩니다.Zxing을 사용하여 QR 코드를 생성하는 시간을 단축하는 방법

더 빨라질 수 있습니까? 또는 페이지가 오래 걸리는 것이 정상입니까? 다른 부분 적재는 1 초 이하로 소요됩니다. 앱도 오프라인이므로 인터넷 연결이 필요하지 않습니다.

ImageView imageViewBarcode = (ImageView)findViewById(R.id.imageViewBarcode); 

    try { 
     bitmap = TextToImageEncode(barcode_user); 

     imageViewBarcode.setImageBitmap(bitmap); 

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

위의 그 코드에서 onCreate 안에 넣어 :

여기 내 코드는 QR 바코드를 생성합니다. 따라서 페이지가로드되면 바코드가 생성됩니다. 여기

기능은 바코드

Bitmap TextToImageEncode(String Value) throws WriterException { 
    BitMatrix bitMatrix; 
    try { 
     bitMatrix = new MultiFormatWriter().encode(
       Value, 
       BarcodeFormat.DATA_MATRIX.QR_CODE, 
       QRcodeWidth, QRcodeWidth, null 
     ); 

    } catch (IllegalArgumentException Illegalargumentexception) { 

     return null; 
    } 
    int bitMatrixWidth = bitMatrix.getWidth(); 

    int bitMatrixHeight = bitMatrix.getHeight(); 

    int[] pixels = new int[bitMatrixWidth * bitMatrixHeight]; 

    for (int y = 0; y < bitMatrixHeight; y++) { 
     int offset = y * bitMatrixWidth; 

     for (int x = 0; x < bitMatrixWidth; x++) { 

      pixels[offset + x] = bitMatrix.get(x, y) ? 
        getResources().getColor(R.color.colorBlack):getResources().getColor(R.color.colorWhite); 
     } 
    } 
    Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444); 

    bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight); 
    return bitmap; 
} 

답변

7

당신의 GetResources 호출을 만드는() 더블 루프 내부 getColor에서을() -. 즉, 이미지의 크기가이 10000 번 호출됩니다 100 개 * 100 픽셀 때. 대신 루프 외부의 일부 변수에 색상 값을 할당하고 루프 내에서 이러한 변수를 사용하십시오.

int color_black = getResources().getColor(R.color.colorBlack); 
int color_white = getResources().getColor(R.color.colorWhite); 

for (int y = 0; y < bitMatrixHeight; y++) { 
    int offset = y * bitMatrixWidth; 

    for (int x = 0; x < bitMatrixWidth; x++) { 
     pixels[offset + x] = bitMatrix.get(x, y) ? color_black : color_white; 
    } 
} 

편집 : 추가 된 코드 예제이 발견

+0

나는 그것을 성취하는 방법을 잘 모른다. 코드 스 니펫 업데이트를 제공 하시겠습니까? – Gabriel

+1

당신이 이해하지 못했던 것을 설명해 주시겠습니까? – Okas

+0

고마워요 @ 오카! 나는 그것을 시도 할 것이다, 늦은 응답에 대해 유감스럽게 생각한다. 나는 그것이 다시 작동한다면 게시 할 것이다. – Gabriel

0

: 여기에 다른 스레드에 zxing generate QR. 나를 위해 비슷한 문제가 해결되었습니다.