一、知识点
- Camerax中的
imageCapture用例默认的image格式是JPEG,
而ImageAnalysis用例默认的image格式是YUV_420_888.
二、ImageAnalysis用例中ImageProxy转mat
YUV转Mat
三、imageCapture中image专byte[]
如下边代码,
//拍照,保存到内存
private void takephoto(){imageCapture.takePicture(ContextCompat.getMainExecutor(this),new ImageCapture.OnImageCapturedCallback() {@Overridepublic void onCaptureSuccess(ImageProxy image) {ByteBuffer buffer = image.getPlanes()[0].getBuffer();byte[] data = new byte[buffer.capacity()];buffer.get(data);//判断是否正确的转为了jpeg格式的字节数组if (isJpeg(data)) {//继续操作}image.close();}});
}
//判断获取的图像是否是jpeg格式private boolean isJpeg(byte[] data) {if (data == null || data.length < 2) {return false;}// JPEG 文件的开头两个字节应该是 0xFF, 0xD8return data[0] == (byte)0xFF && data[1] == (byte)0xD8;}
四、byte[]转mat, 与mat转byte[]
public void byteMat(byte[] data){int width=image.getWidth();int height=image.getHeight();//byte[]转matMat mat = Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.IMREAD_COLOR);// 使用Imgcodecs.imencode将Mat对象编码为JPEG格式byte[]MatOfByte matbuffer = new MatOfByte(); boolean result = Imgcodecs.imencode("a.jpg", mat, matbuffer);// 检查是否成功编码if (result) {// 获取编码后的字节数组byte[] jpegBytes = matbuffer.toArray();//byte[]转matMat mat2 = Imgcodecs.imdecode(new MatOfByte(jpegBytes), Imgcodecs.IMREAD_COLOR);}
}
五、byte[]转bitmap, mat转bitmap
public void bitMap(byte[] data, Mat new_img){//byte转bitmap,这个有一点问题就是转之后的bitmap方向不正Bitmap bit=BitmapFactory.decodeByteArray(data, 0, data.length);//mat转bitmapBitmap mbitmap = Bitmap.createBitmap(new_img.width(), new_img.height(), Bitmap.Config.ARGB_8888);Utils.matToBitmap(new_img,mbitmap);
}