版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Zxing是Google提供的關(guān)于條碼(一維碼、二維碼)的解析工具,提供了二維碼的生成與解析的方法,現(xiàn)在我簡單介紹一下使用Java利用Zxing生成與解析二維碼Jar包下載:/question/tag/zxing1、二維碼的生成1.1 將Zxing-core.jar 包加入到classpath下(如果是Domino平臺,請參考本人的文庫:/view/e09bd825b7?fr=prin)。1.2 二維碼的生成需要借助MatrixToImageWriter類,該類是由Google提供的,可以將該類拷貝到源碼中,這
2、里我將該類的源碼貼上,可以直接使用。import mon.BitMatrix; import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.awt.image.BufferedImage; public final class MatrixToImageWriter private static final int BLACK = 0xFF; private static fi
3、nal int WHITE = 0xFFFFFFFF; private MatrixToImageWriter() public static BufferedImage toBufferedImage(BitMatrix matrix) int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x width; x+) for
4、 (int y = 0; y sourceWidth | top + height sourceHeight) throw new IllegalArgumentException(Crop rectangle does not fit within image data.); for (int y = top; y top + height; y+) for (int x = left; x left + width; x+) if (image.getRGB(x, y) & 0xFF) = 0) image.setRGB(x, y, 0xFFFFFFFF); / = white this.
5、image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY); this.image.getGraphics().drawImage(image, 0, 0, null); this.left = left; this.top = top; Override public byte getRow(int y, byte row) if (y = getHeight() throw new IllegalArgumentException(Requested row is outside th
6、e image: + y); int width = getWidth(); if (row = null | row.length width) row = new bytewidth; image.getRaster().getDataElements(left, top + y, width, 1, row); return row; Override public byte getMatrix() int width = getWidth(); int height = getHeight(); int area = width * height; byte matrix = new
7、bytearea; image.getRaster().getDataElements(left, top, width, height, matrix); return matrix; Override public boolean isCropSupported() return true; Override public LuminanceSource crop(int left, int top, int width, int height) return new BufferedImageLuminanceSource(image, this.left + left, this.to
8、p + top, width, height); Override public boolean isRotateSupported() return true; Override public LuminanceSource rotateCounterClockwise() int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
9、 BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g = rotatedImage.createGraphics(); g.drawImage(image, transform, null); g.dispose(); int width = getWidth(); return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (
10、left + width), getHeight(), width); 2.3 編寫解析二維碼的實(shí)現(xiàn)代碼try MultiFormatReader formatReader = new MultiFormatReader(); String filePath = C:/Users/Administrator/Desktop/testImage/test.jpg; File file = new File(filePath); BufferedImage image = ImageIO.read(file); LuminanceSource source = new BufferedImageL
11、uminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, UTF-8); Result result = formatReader.decode(binaryBitmap,hints); System.out.println(result = + result.toString(); System.out.println(resultFormat = + result.getBarcodeFormat(); System.out.println(resultText = + result.getText(); catch (Exception e) e.pri
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 建筑工程租地合同范本
- 寧夏燴面采購合同范本
- 夾層玻璃做工合同范本
- 大閘蟹供貨協(xié)議書模板
- 建筑工地抽水合同協(xié)議
- 如何簽署安全協(xié)議合同
- 委托定向投資合同范本
- 廣東椰木采購合同范本
- 房屋貸款銀行合同范本
- 高中生物新人教版必修從生物圈到細(xì)胞教案(2025-2026學(xué)年)
- 2026年遼寧生態(tài)工程職業(yè)學(xué)院單招綜合素質(zhì)考試題庫附答案詳解
- 基坑回填質(zhì)量控制措施
- 合作購車位協(xié)議書
- 2025重慶城口縣國有企業(yè)公開招聘26人參考題庫附答案
- 咨政類課題申報書
- 產(chǎn)科護(hù)士長2025年度述職報告
- 2026五個帶頭發(fā)言材料三
- 總承包管理實(shí)施指南
- DB3205-T 1123-2024 職業(yè)教育集團(tuán)建設(shè)與運(yùn)行規(guī)范
- 2025年鐵路職業(yè)技能競賽線路工理論考試試題庫答案
- 鋼結(jié)構(gòu)加固施工方案及施工工藝流程方案
評論
0/150
提交評論