2014-07-22 3 views
2

tiff에서 pdf417 바코드를 읽을 필요가 있습니다.자바에서 zxing으로 하나 이상의 바코드 스캐닝

InputStream in = null; 
     BufferedImage bfi = null; 
     File[] files = new File(DIR).listFiles(); 

     for (int i = 0; i < files.length; i++) { 
      if (files[i].isFile()) { 
       try { 
        System.out.println(files[i].getName()); 
        in = new FileInputStream(files[i]); 
        bfi = ImageIO.read(in); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        if (in != null) { 
         try { 
          in.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 

       if (bfi != null) { 
        LuminanceSource ls = new BufferedImageLuminanceSource(bfi); 
        BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls)); 
        Reader reader = new MultiFormatReader(); 
        Result result = null; 

        Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>(); 
        decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); 

        try { 
         result = reader.decode(bmp, decodeHints); 
        } catch (NotFoundException e) { 
         e.printStackTrace(); 
        } catch (ChecksumException e) { 
         e.printStackTrace(); 
        } catch (FormatException e) { 
         e.printStackTrace(); 
        } 
        System.out.println(result.getBarcodeFormat()); 
        System.out.println("Text " + result.getText()); 
        System.out 
          .println("-------------------------------------------------------"); 

       } else { 
        System.out.println("No Buffered Image for " 
          + files[i].getName()); 
       } 
      } 

     } 

이 가끔 작동하지만, 때로는 그렇지 않은 그 결과가 null 다음 화상 -

에 둘 이상의 바코드이 내 코드 일반적으로있다.

나는 zxing의 javadoc을보고 GenericMultipleBarcodeReader를 발견했다. 나는 내 코드에서의 사용하려고하지만 NullPointerException이 얻을 수 있기 때문에 잘못하고 있어요 :

Reader reader = new MultiFormatReader(); 
        GenericMultipleBarcodeReader greader = new GenericMultipleBarcodeReader(reader); 
        Result[] result = null; 

        Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>(); 
        decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); 

        try { 
         result = greader.decodeMultiple(bmp, decodeHints); 
        } catch (NotFoundException e) { 
         e.printStackTrace(); 
        } 

        for (int j = 0; j < result.length; j++) { 
         System.out.println(result[j].getBarcodeFormat()); 
         System.out.println("Text " + result[j].getText()); 
        } 


Exception in thread "main" java.lang.NullPointerException 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.translateResultPoints(GenericMultipleBarcodeReader.java:163) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:96) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:148) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.decodeMultiple(GenericMultipleBarcodeReader.java:65) 
    at barcode.ZXingTest.main(ZXingTest.java:77) 

그래서 질문은 : 더 나은에 여러 개의 바코드를 스캔 할 GenericMultipleBarcodeReader (또는 다른 클래스)를 사용하는 것입니다 이미지가 있다면 그걸 어떻게 구현해야합니까?

업데이트 : 나는 ByQuadrantReader없이하려고하면

for (int i = 0; i < files.length; i++) { 
      try (BufferedInputStream bfin = new BufferedInputStream(
        new FileInputStream(files[i]))) { 
       dateiname = files[i].getName(); 

       bfi = ImageIO.read(bfin); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      if (bfi != null) { 
       LuminanceSource ls = new BufferedImageLuminanceSource(bfi); 
       BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls)); 

       Reader reader = new MultiFormatReader(); 
       GenericMultipleBarcodeReader greader = new GenericMultipleBarcodeReader(
         new ByQuadrantReader(reader)); 
       Result[] result = null; 

       Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>(); 
       decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); 

       try { 
        result = greader.decodeMultiple(bmp, decodeHints); 
       } catch (NotFoundException e) { 
        e.printStackTrace(); 
        System.out.println("No result"); 
        System.out.println("+++++++++++++++++++++++++++++++"); 
       } 
       if (result != null) { 
        for (int j = 0; j < result.length; j++) { 
         System.out.println(result[j].getText()); 
         System.out.println("+++++++++++++++++++++++++++++++"); 
        } 
       } 

      } else { 
       System.out.println("No Buffered Image for " 
         + files[i].getName()); 
      } 

     } 

, 나는 같은 NullPointerException이 얻을. 나는 그것을하고있는 방법, result.length 때로는 1 (한 문자열 반환) 및 때때로 NotFoundException 얻을. 나는 그것이 내가 볼 수없는 내 코드에서 바보 같은 잘못이 아니에요 희망

...

2 편집 :

Exception in thread "main" java.lang.NullPointerException 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:109) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:148) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.decodeMultiple(GenericMultipleBarcodeReader.java:65) 
    at barcode.ZXingTestMulti.main(ZXingTestMulti.java:86) 

3 편집 : 난

current version of code: 

public static void main(final String[] args) { 

     BufferedImage bfi = null; 
     File[] files = new File(DIR).listFiles(); 
     int counttiffs = 0; 
     String filename = null; 
     TreeMap<String, Exception> errormap = new TreeMap<String, Exception>(); 
     int onebarcode = 0; 
     int twobarcodes = 0; 
     int threebarcodes = 0; 

     for (int i = 0; i < files.length; i++) { 
      if (files[i].isFile()) { 
       try (BufferedInputStream bfin = new BufferedInputStream(
         new FileInputStream(files[i]))) { 
        filename = files[i].getName(); 

        bfi = ImageIO.read(bfin); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       if (bfi != null) { 
        LuminanceSource ls = new BufferedImageLuminanceSource(bfi); 
        BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls)); 

        Reader reader = new MultiFormatReader(); 
        GenericMultipleBarcodeReader greader = new GenericMultipleBarcodeReader(
          reader); 
        Result[] result = null; 

        Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>(); 
        decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); 

        try { 
         result = greader.decodeMultiple(bmp, decodeHints); 
        } catch (NotFoundException e) { 
         errormap.put(filename, e); 
        } catch (NullPointerException e) { 
         errormap.put(filename, e); 
        } 
        if (result != null) { 
         switch (result.length) { 
          case 1: 
           onebarcode++; 
           break; 
          case 3: 
           threebarcodes++; 
           break; 
          default: 
           twobarcodes++; 

         } 
         try (BufferedWriter bfwr = new BufferedWriter(
           new FileWriter(FILE, true))) { 
          bfwr.write(filename + ": number of barcodes found = " 
            + result.length); 
          bfwr.newLine(); 
          for (int j = 0; j < result.length; j++) { 
           bfwr.write(result[j].getText()); 
          } 
          bfwr.newLine(); 
          bfwr.write("---------------------------------------------------------"); 
          bfwr.newLine(); 
          counttiffs++; 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 

        } 

       } 
       else { 
        System.out.println("No Buffered Image for " 
          + files[i].getName()); 
       } 
      } 
     } 

코어 및 자바 스크립트의 스냅 샷 3.1.1을 사용합니다.

java.lang.NullPointerException 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.translateResultPoints(GenericMultipleBarcodeReader.java:163) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:96) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:148) 
    at com.google.zxing.multi.GenericMultipleBarcodeReader.decodeMultiple(GenericMultipleBarcodeReader.java:65) 
    at zBarcodes_Test.ZXingTestMulti.main(ZXingTestMulti.java:72) 

그것은 첫 번째와 동일합니다 : 보시다시피

, 나는 NPE를 잡을 필요가있다. 첫 번째 커밋을 한 후에 다른 줄에 NPE가 있지만 이제는 잘못된 종속성을 사용하거나 다시 발생합니다.

또 다른 점은 : 나는 약 2.500 개의 tiff 파일을 스캔했는데, 각각 2 개의 pdf417 바코드가 있고, 약간 기울어 져 있고 일부는 완벽한 품질이 아닙니다 (일부 pixles는 검은 색 대신 흰색 임). NotFoundException 또는 NullPointer Exception으로 인해 총 1644 개의 오류가 발생합니다. 그리고 218 개의 result.length가 1이고 (바코드 하나만 있음) 68 개로 result.length가 3입니다 (그러나 2 개의 바코드를 스캔해야 함).

바코드가 완벽하지 않고 가장자리에 오류가 거의없고, 픽셀이 완벽하게 인쇄되지 않은 경우 얼마나 유용합니까?

+0

NPE는 버그처럼 들립니다. 당신이 사용하고있는 코드의 버전을 명확히 할 수 있습니까? 컴파일 된 코드의 라인 163은 무엇을 의미합니까? –

+0

나는 그것에 접근 할 수 없다. 이 메소드는 GenericMultipleBarcodeReader가 자동으로 호출됩니다. – user3813234

+0

적어도 어떤 버전의 코드인지 모르시겠습니까? –

답변

1

이것은 단지 버그처럼 보입니다. 나는 https://github.com/zxing/zxing/commit/e715fec42a2f8643c8f53c331f7218a1e62b0dc2에 수정했습니다. 3.1.1-SNAPSHOT을 잡아서 해결할 수 있습니까?

+0

흠, 100 % 확실하지,하지만 치어에 말한다 : 이 com.google.zxing zxing 부모 3.1.1-SNAPSHOT 치어. 이미 3.1.1- 스냅 샷이 없습니까? 아니면 다른 패키지에 대해 이야기하고 있습니까? – user3813234

+0

예, 최신 SNAPSHOT을 참조하고 있음을 의미합니다. 당연히, 나는 그것을 만들기 전에 나의 고침을 가지고 있지 않았을 것이나, 지금 나는 그것을 간청하고 새로운 SNAPSHOT를 간행했다, 당신은 갱신을 얻어야한다. -U 옵션을 사용하여 Maven에 확인을 요청해야 할 수도 있습니다. –

+0

아, 죄송합니다. 지금 당장 수정 사항을 적어 놓았습니다. 나는 그것을 시도하고 다시 당신에게 돌아갈거야. 아마도 내일! 고맙습니다. – user3813234

관련 문제