无法清晰摄像头转换为位图 [英] Unable to clearly convert camera to bitmap
问题描述
当我运行code对camera.set previewCallback接下来的一位,我得到一个静态-Y形象。我怎样才能获得ImageView的(四)正确显示?我需要在这里发表更多code?请温柔,因为这是我的第一篇关于计算器 - 而且,是的,我找啊找,但没有发现任何东西,所以如果你发现一些已经问一个问题,很多在此先感谢
When I run the following bit of code on camera.setPreviewCallback, I get a static-y image. How can I get the ImageView (iv) to display correctly? Do I need to post more code here? Please be gentle as this is my first post on stackoverflow -- And, yes, I searched and searched, but didn't find anything, so if you find something that already asks the question, much thanks in advance.
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, 300, 300, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0,0,300,300), 0, out);
byte[] imageBytes = out.toByteArray();
bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
iv.setImageBitmap(bitmap);
哦,是的,视口300×300。
Oh, and yes, the ViewPort is 300x300.
我甚至试过这个code:
I even tried this code:
int[] imageBytes = convertYUV420_NV21toRGB8888(data, 300, 300);
bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
bitmap.setPixels(imageBytes, 0, 300, 0, 0, 300, 300);
它使用的计算器上的功能之一。
which uses one of the functions on stackoverflow.
推荐答案
我找到了解决办法。宽度和高度是不正确的。即使我有大小的一切,300x300的,解析问题引起的不被尺寸需要,所以这code终于摸索:
I found the solution. The width and height were incorrect. Even though I had sized everything as 300x300, resolution issues caused that to not be the dimensions needed, so this code finally worked:
Camera.Parameters parameters = camera.getParameters();
YuvImage im = new YuvImage(data, ImageFormat.NV21, parameters.getPreviewSize().width, parameters.getPreviewSize().height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
im.compressToJpeg(new Rect(0,0,parameters.getPreviewSize().width, parameters.getPreviewSize().height), 100, out);
byte[] imageBytes = out.toByteArray();
bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, out.size());
iv.setImageBitmap(bitmap);
这篇关于无法清晰摄像头转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!