Android 相机 - 我如何拍摄特定的“矩形"?从字节数组? [英] Android Camera - How can i take a specific "rectangle" from the byte array?
问题描述
我创建了一个拍摄和保存照片的应用程序.我在该预览之上有一个预览和一个叠加层.叠加层定义了一个正方形(正方形周围的区域显示预览有点暗),如图所示:
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.
我尝试了以下方法:
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();
然后将数组square"写入新文件...问题是我得到了一张由线条组成的图片......我所做的转换存在问题
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
推荐答案
我花了一些时间才弄清楚代码应该是这样的:
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屋!