熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

解析圖片1D/2D碼(條形碼)

2022-06-13   來源: Java核心技術 

  看到一個有趣的東西可以解析條碼的工具庫

  ZXING

  測試了一下還可以基本上清晰圖片都可以識別

  Java代碼

  import javaawtFrame;

  import javaawtGraphicsD;

  import javaawtImage;

  import javaawtMediaTracker;

  import javaawtimageBufferedImage;

  import comgooglezxingMonochromeBitmapSource;

  import comgooglezxingMultiFormatReader;

  import comgooglezxingReader;

  import comgooglezxingReaderException;

  import comgooglezxingResult;

  import comgooglezxingclientjseBufferedImageMonochromeBitmapSource;

  public class CodeReader {

  /**

  * @param args

  * @throws ReaderException

  * @throws InterruptedException

  */

  public static void main(String[] args) throws ReaderException InterruptedException {

  Reader reader = new MultiFormatReader();

  String imgPath =

  testjpg;

  Image image=javaawtToolkitgetDefaultToolkit()getImage(imgPath);

  BufferedImage myImage = CodeReaderimageToBufImage(image);

  MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(myImage);

  Result result = readerdecode(source);

  Systemoutprintln(resultgetText());

  }

  public static BufferedImage imageToBufImage(Image image) throws InterruptedException{

  MediaTracker mt=new MediaTracker(new Frame());

  mtaddImage(image);

  mtwaitForID();

  BufferedImage bufImage=new BufferedImage(imagegetWidth(null)imagegetHeight(null)BufferedImageTYPE_INT_BGR);

  GraphicsD   gd=bufImagecreateGraphics();

  gddrawImage(imagenull);

  return bufImage;

  }

  }

   

  

  


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26474.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.