|
| 1 | +/* |
| 2 | + * This Java source file was generated by the Gradle 'init' task. |
| 3 | + */ |
| 4 | +package dynamsoft.barcode.reader; |
| 5 | + |
| 6 | +import com.dynamsoft.dbr.*; |
| 7 | + |
| 8 | +import java.nio.file.*; |
| 9 | + |
| 10 | +public class App { |
| 11 | + |
| 12 | + private BarcodeReader mBarcodeReader; |
| 13 | + |
| 14 | + public App(String license) { |
| 15 | + try { |
| 16 | + mBarcodeReader = new BarcodeReader(license); |
| 17 | + } catch (Exception e) { |
| 18 | + //TODO: handle exception |
| 19 | + e.printStackTrace(); |
| 20 | + } |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + public BarcodeReader getBarcodeReader() { |
| 25 | + return mBarcodeReader; |
| 26 | + } |
| 27 | + |
| 28 | + public String getGreeting() { |
| 29 | + return "Hello Dynamsoft Barcode Reader!"; |
| 30 | + } |
| 31 | + |
| 32 | + public TextResult[] decodeFile(String file) { |
| 33 | + TextResult[] results = null; |
| 34 | + // Read barcode |
| 35 | + try { |
| 36 | + //Best coverage settings |
| 37 | + // br.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestCoverage\",\"DeblurLevel\":9,\"ExpectedBarcodesCount\":512,\"ScaleDownThreshold\":100000,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_SCAN_DIRECTLY\"},{\"Mode\":\"LM_STATISTICS\"},{\"Mode\":\"LM_LINES\"},{\"Mode\":\"LM_STATISTICS_MARKS\"}],\"GrayscaleTransformationModes\":[{\"Mode\":\"GTM_ORIGINAL\"},{\"Mode\":\"GTM_INVERTED\"}]}}", EnumConflictMode.CM_OVERWRITE); |
| 38 | + //Best speed settings |
| 39 | + //br.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestSpeed\",\"DeblurLevel\":3,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_SCAN_DIRECTLY\"}],\"TextFilterModes\":[{\"MinImageDimension\":262144,\"Mode\":\"TFM_GENERAL_CONTOUR\"}]}}",EnumConflictMode.CM_OVERWRITE); |
| 40 | + //Balance settings |
| 41 | + mBarcodeReader.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"Balance\",\"DeblurLevel\":5,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_STATISTICS\"}]}}",EnumConflictMode.CM_OVERWRITE); |
| 42 | + long start = System.currentTimeMillis(); |
| 43 | + results = mBarcodeReader.decodeFile(file, ""); |
| 44 | + long end = System.currentTimeMillis(); |
| 45 | + |
| 46 | + if (results == null || results.length == 0) { |
| 47 | + System.out.println(String.format("No barcode found. Total time cost: %d ms.", (end - start))); |
| 48 | + } else { |
| 49 | + System.out.println(String.format("Total barcode(s) found: %d. Total time cost: %d ms.", results.length, (end -start))); |
| 50 | + int index = 0; |
| 51 | + for (TextResult result : results) { |
| 52 | + System.out.println(String.format(" Barcode %d:", index++)); |
| 53 | + if(result.barcodeFormat != 0){ |
| 54 | + System.out.println(" Type: " + result.barcodeFormatString); |
| 55 | + } else { |
| 56 | + System.out.println(" Type: " + result.barcodeFormatString_2); |
| 57 | + } |
| 58 | + |
| 59 | + System.out.println(" Value: " + result.barcodeText); |
| 60 | + |
| 61 | + System.out.println(String.format(" Region points: {(%d,%d),(%d,%d),(%d,%d),(%d,%d)}", |
| 62 | + result.localizationResult.resultPoints[0].x, result.localizationResult.resultPoints[0].y, |
| 63 | + result.localizationResult.resultPoints[1].x,result.localizationResult.resultPoints[1].y, |
| 64 | + result.localizationResult.resultPoints[2].x,result.localizationResult.resultPoints[2].y, |
| 65 | + result.localizationResult.resultPoints[3].x,result.localizationResult.resultPoints[3].y)); |
| 66 | + } |
| 67 | + } |
| 68 | + } catch (BarcodeReaderException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + |
| 72 | + return results; |
| 73 | + } |
| 74 | + |
| 75 | + public static void main(String[] args) throws Exception { |
| 76 | + |
| 77 | + System.out.println("Working Directory = " + System.getProperty("user.dir")); |
| 78 | + |
| 79 | + if (args.length == 0) { |
| 80 | + String newLine = System.getProperty("line.separator"); |
| 81 | + String s = new StringBuilder() |
| 82 | + .append("Usage:") |
| 83 | + .append(newLine) |
| 84 | + .append(" ./gradlew run --args=\"<image file> <license file>\"").append(newLine) |
| 85 | + .toString(); |
| 86 | + |
| 87 | + System.out.println(s); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + String file = args[0]; |
| 92 | + String license = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="; |
| 93 | + |
| 94 | + if (args.length == 2) { |
| 95 | + |
| 96 | + license = new String(Files.readAllBytes(Paths.get(args[1]))); |
| 97 | + } |
| 98 | + App app = new App(license); |
| 99 | + System.out.println(app.getGreeting()); |
| 100 | + app.decodeFile(file); |
| 101 | + |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments