Android摄像头 - 我如何可以采取一个特定的"矩形"从字节数组? [英] Android Camera - How can i take a specific "rectangle" from the byte array?
问题描述
我已经创建了拍摄并保存照片的应用程序。 我有一个preVIEW和重要的是preVIEW顶部覆盖。 叠加定义了一个正方形(广场周围的区域显示了preVIEW有点暗),因为你可以在图片中看到:
I have created an application that shoots and saves photos. I have a preview and an overlay on top of that preview. The overlay defines a square (the area around the square shows the preview a bit darker), as you can see in the image:
我需要做的是提取其中方是图像的一部分。 方形的定义是这样的:
What i need to do is to extract the part of the image where the square is. The square is defined like this:
Rect frame = new Rect(350,50,450,150);
我怎样才能做到这一点?我有一个字节数组(byte []的数据),我可以节省,但我想更改应用程序,以便只有方形区域将被保存。
How can i do that? i have the byte array (byte[] data) that i can save, but i want to change the application so that only the square area will be saved.
编辑:我已经试过如下:
i have tried the following:
int[] pixels = new int[100000];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
bitmap.getPixels(pixels, 0, 480, 350, 50, 100, 100);
bitmap = Bitmap.createBitmap(pixels, 0, 100, 100, 100, Config.ARGB_4444);
bitmap.compress(CompressFormat.JPEG, 0, bos);
byte[] square = bos.toByteArray();
,然后写入阵列广场,以一个新的文件... 问题是,我得到发行的画面......有一个与我所做的转型问题
and then write the array "square" to a new file... The problem is that i get a picture made of lines... there is a problem with the transformation that i made
推荐答案
我花了一些时间来弄清楚,在code应该是这样的:
It took me some time to figure out that the code should look like this:
int[] pixels = new int[100*100];//the size of the array is the dimensions of the sub-photo
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
bitmap.getPixels(pixels, 0, 100, 350, 50, 100, 100);//the stride value is (in my case) the width value
bitmap = Bitmap.createBitmap(pixels, 0, 100, 100, 100, Config.ARGB_8888);//ARGB_8888 is a good quality configuration
bitmap.compress(CompressFormat.JPEG, 100, bos);//100 is the best quality possibe
byte[] square = bos.toByteArray();
这篇关于Android摄像头 - 我如何可以采取一个特定的"矩形"从字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!