|
| 1 | +package com.java.barcode; |
| 2 | + |
| 3 | +import com.dynamsoft.dbr.*; |
| 4 | +import com.google.zxing.BinaryBitmap; |
| 5 | +import com.google.zxing.MultiFormatReader; |
| 6 | +import com.google.zxing.NotFoundException; |
| 7 | +import com.google.zxing.RGBLuminanceSource; |
| 8 | +import com.google.zxing.Result; |
| 9 | +import com.google.zxing.common.HybridBinarizer; |
| 10 | +import com.google.zxing.multi.*; |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.awt.image.*; |
| 14 | +import javax.imageio.ImageIO; |
| 15 | + |
| 16 | +public class App |
| 17 | +{ |
| 18 | + public void decodefile(String filename) { |
| 19 | + // Read an image to BufferedImage |
| 20 | + BufferedImage image = null; |
| 21 | + try { |
| 22 | + image = ImageIO.read(new File(filename)); |
| 23 | + } catch (IOException e) { |
| 24 | + System.out.println(e); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // ZXing |
| 29 | + BinaryBitmap bitmap = null; |
| 30 | + int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth()); |
| 31 | + RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels); |
| 32 | + bitmap = new BinaryBitmap(new HybridBinarizer(source)); |
| 33 | + |
| 34 | + MultiFormatReader reader = new MultiFormatReader(); |
| 35 | + GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader); |
| 36 | + try { |
| 37 | + Result[] zxingResults = multiReader.decodeMultiple(bitmap); |
| 38 | + System.out.println("ZXing result count: " + zxingResults.length); |
| 39 | + if (zxingResults != null) { |
| 40 | + for (Result zxingResult : zxingResults) { |
| 41 | + System.out.println("Format: " + zxingResult.getBarcodeFormat()); |
| 42 | + System.out.println("Text: " + zxingResult.getText()); |
| 43 | + System.out.println(); |
| 44 | + } |
| 45 | + } |
| 46 | + } catch (NotFoundException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + pixels = null; |
| 50 | + bitmap = null; |
| 51 | + |
| 52 | + System.out.println("------------------------------------------------------"); |
| 53 | + |
| 54 | + |
| 55 | + // Dynamsoft |
| 56 | + BarcodeReader br = null; |
| 57 | + try { |
| 58 | + // Get a license key from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr |
| 59 | + BarcodeReader.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="); |
| 60 | + br = new BarcodeReader(); |
| 61 | + } catch (Exception e) { |
| 62 | + System.out.println(e); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + TextResult[] results = null; |
| 67 | + try { |
| 68 | + results = br.decodeBufferedImage(image, ""); |
| 69 | + } catch (Exception e) { |
| 70 | + System.out.println("decode buffered image: " + e); |
| 71 | + } |
| 72 | + |
| 73 | + if (results != null && results.length > 0) { |
| 74 | + System.out.println("DBR result count: " + results.length); |
| 75 | + String pszTemp = null; |
| 76 | + for (TextResult result : results) { |
| 77 | + if (result.barcodeFormat != 0) { |
| 78 | + pszTemp = "Format: " + result.barcodeFormatString; |
| 79 | + } else { |
| 80 | + pszTemp = "Format: " + result.barcodeFormatString_2; |
| 81 | + } |
| 82 | + System.out.println(pszTemp); |
| 83 | + System.out.println("Text: " + result.barcodeText); |
| 84 | + System.out.println(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (image != null) { |
| 89 | + image.flush(); |
| 90 | + image = null; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static void main( String[] args ) |
| 95 | + { |
| 96 | + if (args.length == 0) { |
| 97 | + System.out.println("Please add an image file"); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + final String filename = args[0]; |
| 102 | + App test = new App(); |
| 103 | + test.decodefile(filename); |
| 104 | + } |
| 105 | +} |
0 commit comments