zxing library (in scala) not decoding images taken with cell phone's camera

80 views Asked by At

I'm trying to decode pdf417 type barcodes. For this I'm using "zxing" library in scala.

Code:

def decodePDF417FromImage(imageFile: File): Unit = {
    try {
      val image = ImageIO.read(imageFile)
      val source = new BufferedImageLuminanceSource(image)
      val bitmap = new BinaryBitmap(new HybridBinarizer(source))

      val hints = new java.util.HashMap[DecodeHintType, Any]()
      hints.put(DecodeHintType.TRY_HARDER, true)

      val reader = new MultiFormatReader()
      val result: Result = reader.decode(bitmap, hints)

      if (result != null) {
        val decoded = result.getText
        println(decoded)
            }
      else {
        println("No PDF417 Barcode Found!")
      }
    } catch {
      case ex: NotFoundException =>
        println("No PDF417 Barcode Found!")
      case ex: java.lang.NullPointerException =>
        ex.printStackTrace()
      case ex: Exception =>
        ex.printStackTrace()
    }
  }

  val imageFile = new File("path-to-image")
  decodePDF417FromImage(new File(ouputFile))

Above code is working fine if I take screenshot of image from google - but if I take image with my cell phone's camera (opening image in laptop browser/media and capturing image from cell phone) and run above code it always give "No Barcode Found".

I've checked with some online tools, both screenshot image's barcode and cell phone image's barcode are decoding fine. (https://online-barcode-reader.inliteresearch.com/)

Another scenario is, if I capture image from cell phone's camera (which does not work) and open it in laptop's Image Viewer - then take screenshot and run that image. In this case it worked.

I've tried to convert type of image from jpg (cell phone's image) to png (screenshot of laptop) but it does not work as well. Also I tried to enhance image taken from cell phone's camera and then apply zxing but it does not work.

First I think I'm capturing image from laptop's screen that's why it is not working, but on the other hand online tools are giving decoded texts. So I got confused - guide me how to solve above.

Image I've used (https://www.google.com/url?sa=i&url=https%3A%2F%2Fbarcodelive.org%2Fdrivers-license-barcode&psig=AOvVaw2QfoD8_3srZb-D2Q8ZoVpL&ust=1700036039153000&source=images&cd=vfe&opi=89978449&ved=0CBIQjRxqFwoTCPiV4vOFw4IDFQAAAAAdAAAAABAL) - First Image in it with pdf417 barcode.

0

There are 0 answers