|
| 1 | +package com.dynamsoft; |
| 2 | + |
| 3 | +import com.dynamsoft.dbr.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.ThreadPoolExecutor; |
| 10 | +import java.awt.image.*; |
| 11 | +import javax.imageio.ImageIO; |
| 12 | + |
| 13 | +public final class App { |
| 14 | + private App() { |
| 15 | + } |
| 16 | + |
| 17 | + public void decodefile(String filename) { |
| 18 | + int iIndex = 0; |
| 19 | + String pszImageFile = filename; |
| 20 | + BarcodeReader br = null; |
| 21 | + try { |
| 22 | + // Get a license key from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr |
| 23 | + BarcodeReader.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="); |
| 24 | + br = new BarcodeReader(); |
| 25 | + } catch (Exception e) { |
| 26 | + System.out.println(e); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // BufferedImage img = null; |
| 31 | + // try { |
| 32 | + // img = ImageIO.read(new File(pszImageFile)); |
| 33 | + // } catch (IOException e) { |
| 34 | + // System.out.println(e); |
| 35 | + // } |
| 36 | + |
| 37 | + TextResult[] results = null; |
| 38 | + try { |
| 39 | + // results = br.decodeBufferedImage(img, ""); |
| 40 | + results = br.decodeFile(pszImageFile, ""); |
| 41 | + // results = br.decodeFileInMemory(Files.readAllBytes(new File(pszImageFile).toPath()), ""); |
| 42 | + } catch (Exception e) { |
| 43 | + System.out.println("decode buffered image: " + e); |
| 44 | + } |
| 45 | + |
| 46 | + if (results != null && results.length > 0) { |
| 47 | + String pszTemp = null; |
| 48 | + iIndex = 0; |
| 49 | + for (TextResult result : results) { |
| 50 | + iIndex++; |
| 51 | + pszTemp = String.format(" Barcode %d:", iIndex); |
| 52 | + System.out.println(pszTemp); |
| 53 | + pszTemp = String.format(" Page: %d", result.localizationResult.pageNumber + 1); |
| 54 | + System.out.println(pszTemp); |
| 55 | + if (result.barcodeFormat != 0) { |
| 56 | + pszTemp = " Type: " + result.barcodeFormatString; |
| 57 | + } else { |
| 58 | + pszTemp = " Type: " + result.barcodeFormatString_2; |
| 59 | + } |
| 60 | + System.out.println(pszTemp); |
| 61 | + pszTemp = " Value: " + result.barcodeText; |
| 62 | + System.out.println(pszTemp); |
| 63 | + |
| 64 | + pszTemp = String.format(" Region points: {(%d,%d),(%d,%d),(%d,%d),(%d,%d)}", |
| 65 | + result.localizationResult.resultPoints[0].x, result.localizationResult.resultPoints[0].y, |
| 66 | + result.localizationResult.resultPoints[1].x, result.localizationResult.resultPoints[1].y, |
| 67 | + result.localizationResult.resultPoints[2].x, result.localizationResult.resultPoints[2].y, |
| 68 | + result.localizationResult.resultPoints[3].x, result.localizationResult.resultPoints[3].y); |
| 69 | + |
| 70 | + System.out.println(pszTemp); |
| 71 | + System.out.println(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Says hello to the world. |
| 78 | + * @param args The arguments of the program. |
| 79 | + */ |
| 80 | + public static void main(String[] args) { |
| 81 | + if (args.length == 0) { |
| 82 | + System.out.println("Please add an image file"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + final String filename = args[0]; |
| 87 | + |
| 88 | + ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1); |
| 89 | + |
| 90 | + for (int i = 1; i <= 1; i++) |
| 91 | + { |
| 92 | + executor.execute(new Runnable(){ |
| 93 | + |
| 94 | + @Override |
| 95 | + public void run() { |
| 96 | + App test = new App(); |
| 97 | + test.decodefile(filename); |
| 98 | + try { |
| 99 | + Thread.sleep(200); |
| 100 | + } catch (InterruptedException e) { |
| 101 | + System.out.println(e); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + executor.shutdown(); |
| 107 | + } |
| 108 | +} |
0 commit comments