import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.WritableRaster; import java.net.URL; import java.util.List; /** * @program: test-wechatqrcode * @description: 微信二维码扫描opencv库方法调用 * @author: test * @create: 221-08-21 17:05 **/ public class WeChatQRCodeTool { private static volatile WeChatQRCodeTool instance; private static volatile org.opencv.wechat_qrcode.WeChatQRCode detector; //封装程了工具类,所以做成了单例 private WeChatQRCodeTool() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //微信提供的4个模型配置文件放在resource/wechatqrcode文件夹里 URL detectprototxt = ClassLoader.getSystemResource("wechatqrcode/detect.prototxt"); URL detectcaffemodel = ClassLoader.getSystemResource("wechatqrcode/detect.caffemodel"); URL srprototxt = ClassLoader.getSystemResource("wechatqrcode/sr.prototxt"); URL srcaffemodel = ClassLoader.getSystemResource("wechatqrcode/sr.caffemodel"); //实例化微信二维码扫描对象 detector = new org.opencv.wechat_qrcode.WeChatQRCode( detectprototxt.getPath().substring(1), //substring1去掉/D:/xx里的/ detectcaffemodel.getPath().substring(1), srprototxt.getPath().substring(1), srcaffemodel.getPath().substring(1)); } public static WeChatQRCodeTool getInstance() { if (instance == null) { synchronized (WeChatQRCodeTool.class) { if (instance == null) { instance = new WeChatQRCodeTool(); } } } return instance; } /** * 这里提供一个bufferimage的入参。如果是File对象,可以用ImageIO.read()获取一下bufferimage. */ public String decode(BufferedImage srcImage) { Mat image = bufImg2Mat(srcImage, srcImage.getType(), CvType.CV_8UC3); //返回解析的字符串,如果图片有多个二维码,则会返回多个。 List<String> result2 = detector.detectAndDecode(image); if (result2 != null && result2.size() > 0) { return result2.get(0); } return null; } /** * BufferedImage转换成Mat * * @param original 要转换的BufferedImage * @param imgType bufferedImage的类型 如 BufferedImage.TYPE_3BYTE_BGR * @param matType 转换成mat的type 如 CvType.CV_8UC3 */ public static Mat bufImg2Mat(BufferedImage original, int imgType, int matType) { if (original == null) { throw new IllegalArgumentException("original == null"); } // Don't convert if it already has correct type if (original.getType() != imgType) { // Create a buffered image BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), imgType); // Draw the image onto the new buffer Graphics2D g = image.createGraphics(); try { g.setComposite(AlphaComposite.Src); g.drawImage(original, 0, 0, null); } finally { g.dispose(); } } byte[] pixels = ((DataBufferByte) original.getRaster().getDataBuffer()).getData(); Mat mat = Mat.eye(original.getHeight(), original.getWidth(), matType); mat.put(0, 0, pixels); return mat; } /** * Mat转换为BufferedImage */ public static BufferedImage matToBufferedImage(Mat mat) { if (mat.height() > 0 && mat.width() > 0) { BufferedImage image = new BufferedImage(mat.width(), mat.height(), BufferedImage.TYPE_3BYTE_BGR); WritableRaster raster = image.getRaster(); DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); byte[] data = dataBuffer.getData(); mat.get(0, 0, data); return image; } return null; } }
由于wechatqrcode属于第三方扩展库,不属于opencv的主版本,所以opencv自带的发布dll和jar里没有。需要自编译。花了我一天时间。具体参考:
Windows下联合编译opencv+opencv_contrib微信二维码引擎_VQB01799的博客-CSDN博客
不想花费时间编译的,可以使用下面我分享到百度网盘的opencv_java453.dll链接库和opencv-453.jar