我的目标是增加一个覆盖在相机预览,将找到书的边缘。为此,我将重写onPreviewFrame,执行以下操作:
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
Mat mat = new Mat((int) (height*1.5), width, CvType.CV_8UC1);
mat.put(0,0,data);
byte[] bytes = new byte[(int) (height*width*1.5)];
mat.get(0,0,bytes);
if (!test) { //to only do once
File pictureFile = getOutputMediaFile();
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(bytes);
fos.close();
Uri picUri = Uri.fromFile(pictureFile);
updateGallery(picUri);
test = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}现在,我只想做一个预览,并保存在转换后的垫子。
在花了无数个小时让上面的图片看起来正确后,我的测试电话(LG Leon)无法看到保存的图片。我似乎找不到这个问题。我是不是混合了高度/宽度,因为我是在肖像模式下拍照?我试着换了,但还是不起作用。问题出在哪里?
发布于 2016-04-28 13:42:09
看起来效率很低,但对我来说(目前来说)是有效的:
//get the camera parameters
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
//convert the byte[] to Bitmap through YuvImage;
//make sure the previewFormat is NV21 (I set it so somewhere before)
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 70, out);
Bitmap bmp = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
//convert Bitmap to Mat; note the bitmap config ARGB_8888 conversion that
//allows you to use other image processing methods and still save at the end
Mat orig = new Mat();
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp, orig);
//here you do whatever you want with the Mat
//Mat to Bitmap to OutputStream to byte[] to File
Utils.matToBitmap(orig, bmp);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);
byte[] bytes = stream.toByteArray();
File pictureFile = getOutputMediaFile();
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(bytes);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}发布于 2016-04-27 19:56:13
我找到的最快的方法是在我最近提出的问题中描述HERE。你可以在我在下面的问题中写的答案中找到提取图像的方法。问题是,您通过onPreviewFrame()获得的图像是NV21。在收到此图像后,可能需要将其转换为RGB (取决于您想要实现什么;这也是在我之前给您的答案中所做的)。
https://stackoverflow.com/questions/36892934
复制相似问题