解析二维码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | package www.ablanxue.com; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.exception.DecodingFailedException; public class QRCode { /** * 解析二维码(QRCode) * @param imgPath 图片路径 * @return */ public String decoderQRCode(String imgPath) { // QRCode 二维码图片的文件 File imageFile = new File(imgPath); BufferedImage bufImg = null ; String content = null ; try { bufImg = ImageIO.read(imageFile); QRCodeDecoder decoder = new QRCodeDecoder(); content = new String(decoder.decode( new QRCodeImageBean(bufImg)), "utf-8" ); } catch (IOException e) { System.out.println( "Error: " + e.getMessage()); e.printStackTrace(); } catch (DecodingFailedException dfe) { System.out.println( "Error: " + dfe.getMessage()); dfe.printStackTrace(); } return content; } public static void main(String[] args) { String imgPath = "K:/aaa.png" ; QRCode handler = new QRCode(); String qrCon = handler.decoderQRCode(imgPath); System.out.println( "decoder success!!!" ); System.out.println( "二维码内容为:" + qrCon); } } QRCodeImageBean.java package www.ablanxue.com; import java.awt.image.BufferedImage; import jp.sourceforge.qrcode.data.QRCodeImage; public class QRCodeImageBean implements QRCodeImage { BufferedImage bufImg; public QRCodeImageBean(BufferedImage bufImg) { this .bufImg = bufImg; } @Override public int getHeight() { return bufImg.getHeight(); } @Override public int getPixel( int x, int y) { return bufImg.getRGB(x, y); } @Override public int getWidth() { return bufImg.getWidth(); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步