java操作二维码生成与已有图片合成与解析+pc摄像头抓拍

编程入门 行业动态 更新时间:2024-10-11 23:21:30

java操作二维码生成与<a href=https://www.elefans.com/category/jswz/34/1764632.html style=已有图片合成与解析+pc摄像头抓拍"/>

java操作二维码生成与已有图片合成与解析+pc摄像头抓拍

java操作二维码生成与已有图片合成与解析+pc摄像头抓拍

效果

pom中依赖

 

    <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version></dependency><dependency><groupId>org.apachemons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>javacv-platform</artifactId><version>1.4.3</version></dependency>

代码实现

  • Camera类

     

    import org.bytedeco.javacv.OpenCVFrameGrabber;public class Camera {/*** 是否存储图片*/public static enum Img {save,nosave}/*** 是否正常启动摄像头 true:正常启动/false:异常启动*/public static boolean InitCamera;/*** 摄像头资源*/public static OpenCVFrameGrabber grabber;/*** 是否保存图片,默认不保存*/public static Img saveImage = Img.nosave;/*** 开始摄像头** @throws Exception 摄像头初始化失败!*/public static void StartCamera() throws Exception {grabber.start();}/*** 停止摄像头** @throws Exception 摄像头关闭异常!*/public static void StopCamera() throws Exception {grabber.stop();}
    }
    

工具类

 

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class BaseUtil {/*** 二维码信息和图片合成新的图片** @param qrInfo    二维码文字信息内容* @param imagePath 输入图片文件路径* @param toPath    合成图片文件路径【注:路径后缀为.png】* @param cachePath 缓存目录路径* @return 是否操作成功*/public static boolean createQrMergeImage(String qrInfo, String imagePath, String toPath, String cachePath) {try {//判断二维码信息是否为空if (qrInfo == null || "".equals(qrInfo.trim())) {System.err.println("Parameter \'qrInfo\' cannot be empty");return false;}//判断图片路径是否为空if (imagePath == null || "".equals(imagePath.trim())) {System.err.println("Parameter \'imagePath\' cannot be empty");return false;}//判断图片文件是否存在File imageFile = new File(imagePath);if (!imageFile.exists()) {System.err.println("The image file is not exits");return false;}//判断缓存目录是否为空if (cachePath == null || "".equals(cachePath.trim())) {System.err.println("Parameter \'cachePath\' cannot be empty");return false;}//判断缓存目录是否存在File cacheFile = new File(cachePath);if (!cacheFile.exists() || !cacheFile.isDirectory()) {System.err.println("cachePath is not exits or is not directory");return false;}//判断输出文件路径是否为空if (toPath == null || "".equals(toPath.trim())) {System.err.println("Parameter \'toPath\' cannot be empty");return false;}//判断输出文件路径是否.png结尾if (!toPath.endsWith(".png")) {System.err.println("Parameter \'toPath\' is not end \'.png\'");return false;}//判断输出文件是否存在File toFile = new File(toPath);if (!toFile.exists()) {toFile.createNewFile();}//判断是否为图片try {Image image = ImageIO.read(imageFile);if (image == null) {System.err.println("The image file is not real picture");return false;}} catch (IOException ex) {System.err.println("The image file is not real picture");return false;}//设置ImageIO的缓存目录ImageIO.setCacheDirectory(cacheFile);//获取图片的高BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imageFile));int height = bufferedImage.getHeight();if (height <= 0) {System.err.println("Get image file hight error");return false;}//生成等高的二维码MultiFormatWriter multiFormatWriter = new MultiFormatWriter();Map hints = new HashMap();//设置UTF-8, 防止中文乱码hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置二维码四周白色区域的大小hints.put(EncodeHintType.MARGIN, 0);//设置二维码的容错性hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//画二维码,记得调用multiFormatWriter.encode()时最后要带上hints参数,不然上面设置无效BitMatrix bitMatrix = multiFormatWriter.encode(qrInfo, BarcodeFormat.QR_CODE, height, height, hints);//开始画二维码BufferedImage bufferedImageQr = MatrixToImageWriter.toBufferedImage(bitMatrix);//判断生成的二维码是否为空if (bufferedImageQr == null) {System.err.println("Create Buffer Image Qr error");return false;}//合并图片与生成的二维码int w1 = bufferedImage.getWidth();int h1 = bufferedImage.getHeight();int w2 = bufferedImageQr.getWidth();int h2 = bufferedImageQr.getHeight();// 从图片中读取RGBint[] ImageArrayOne = new int[w1 * h1];ImageArrayOne = bufferedImage.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中int[] ImageArrayTwo = new int[w2 * h2];ImageArrayTwo = bufferedImageQr.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);// 生成新图片BufferedImage DestImage = null;DestImage = new BufferedImage(w1 + w2, h1, BufferedImage.TYPE_INT_RGB);DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);ImageIO.write(DestImage, "png", new File(toPath));return true;} catch (Exception e) {return false;}}/*** 解析图片中的二维码文本信息** @param imagePath 含二维码图片的路径* @param cachePath 缓存目录路径* @return 解析出的二维码文本信息*/public static String decodeQRInfoFromImage(String imagePath, String cachePath) {String qrInfo = null;try {//判断图片路径是否为空if (imagePath == null || "".equals(imagePath.trim())) {System.err.println("Parameter \'imagePath\' cannot be empty");qrInfo = null;return qrInfo;}//判断图片文件是否存在File imageFile = new File(imagePath);if (!imageFile.exists()) {System.err.println("The image file is not exits");qrInfo = null;return qrInfo;}//判断是否为图片try {Image image = ImageIO.read(imageFile);if (image == null) {System.err.println("The image file is not real picture");qrInfo = null;return qrInfo;}} catch (IOException ex) {System.err.println("The image file is not real picture");qrInfo = null;return qrInfo;}//判断缓存目录是否为空if (cachePath == null || "".equals(cachePath.trim())) {System.err.println("Parameter \'cachePath\' cannot be empty");qrInfo = null;return qrInfo;}//判断缓存目录是否存在File cacheFile = new File(cachePath);if (!cacheFile.exists() || !cacheFile.isDirectory()) {System.err.println("cachePath is not exits or is not directory");qrInfo = null;return qrInfo;}ImageIO.setCacheDirectory(new File(cachePath));BufferedImage image = ImageIO.read(new File(imagePath));LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");com.google.zxing.Result result = new MultiFormatReader().decode(binaryBitmap, hints);qrInfo = result.getText();return qrInfo;} catch (Exception e) {qrInfo = null;return qrInfo;}}/*** 调用相机拍照** @param imagePath    存储拍摄照片的路径* @param cameraNumber 相机的编号【-1手动选择】【0表示本机相机】【1~n表示usb摄像头(这里限制n最大为5)】* @return 是否完成拍照*/public static boolean takePhoto(String imagePath, int cameraNumber) {try {//判断图片路径是否为空if (imagePath == null || "".equals(imagePath.trim())) {System.err.println("Parameter \'imagePath\' cannot be empty");return false;}//判断相机参数是否越界if (cameraNumber < -1 || cameraNumber > 5) {System.err.println("Parameter \'cameraNumber\' out range");return false;}//拍照并保存Camera.grabber = new OpenCVFrameGrabber(cameraNumber);Camera.StartCamera();Camera.InitCamera = true;OpenCVFrameGrabber grabber = Camera.grabber;OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();try {boolean imwrite = false;if (Camera.InitCamera) {opencv_core.Mat mat = converter.convertToMat(grabber.grabFrame());Camera.saveImage = Camera.Img.save;if (Camera.saveImage == Camera.Img.save) {imwrite = opencv_imgcodecs.imwrite(imagePath, mat);}}Camera.StopCamera();return imwrite;} catch (Exception e) {System.err.println("take photo error");return false;}} catch (Exception e) {System.err.println("init camera error");Camera.InitCamera = false;return false;}}
}

更多推荐

java操作二维码生成与已有图片合成与解析+pc摄像头抓拍

本文发布于:2024-03-04 04:15:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1708243.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:已有   摄像头   操作   图片   二维码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!