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();
// 从图片中读取RGB
int[] 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;
}
}
}