从字节数组裁剪图像 [英] Crop image from byte array
问题描述
我的应用程序允许用户用相机拍照并返回一个包含图像数据的字节数组(目前为jpeg,但可以修改).
My app let the user take a picture with the camera and return a byte array containing the image data (jpeg at the moment but can be modified).
如果我们知道图片的宽度和高度,可以使用
If we know the width and height of the picture, is it possible to use
BitmapFactory.decodeByteArray(data, offset, length)
要从指定的偏移量解码图片以获得平方的图片?
To decode the picture from specified offset to get a squared picture ?
图像具有高分辨率(人像),我只需要这张照片的方形底部仍然具有高分辨率就可以进行图像处理.
The image is in high res (portrait) and I only need the squared bottom of this picture still in high res for image processing.
x offset = 0
y offset = height - width
width = width
height = width
我已经看到了这个答案,但我仍然想知道这是否是可能的.如果需要,我可以使用其他格式压缩图像数据
I have seen this answer but I still was wondering if this was possible. I could compress image data with another format if needed
我的猜测是计算用于存储n
像素行的字节数(使用已知尺寸),其中n = maxRows - pictureWidth
My guess was to compute the number of bytes used to store n
rows of pixels (using known dimensions), where n = maxRows - pictureWidth
然后从计算出的偏移量开始
and then start from computed offset
推荐答案
首先解码为小尺寸
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 4; //inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels
BitmapFactory.decodeByteArray(data, offset, length, opt)
然后
bitMap1 = Bitmap.createScaledBitmap(bitMap, width, height, true);
尝试
这篇关于从字节数组裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!