如何调整android.media.Image的大小

时间:2018-02-10 01:05:35

标签: android android-image

如何动态调整android.media.Image的大小?

我正在使用的Image很小,可以录制,但我需要暂时将图像拉伸到更大的比例进行分析。

Image image = mImageReader.acquireNextImage();

// ...
// Resize image to a larger size in memory. Quality is not of a concern.
// ...

image.close();

任何想法都将不胜感激。感谢。

1 个答案:

答案 0 :(得分:-1)

首先使用此方法convertImageToBitmap()将图片转换为位图,然后使用下面给出的getResizedBitmap()方法调整位图大小。我希望这会对你有所帮助。

转换位图:

private Bitmap convertImageToBitmap(Image image) {

    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[buffer.capacity()];
    buffer.get(bytes);
    Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);

    return bitmapImage;
}

使用以下内容调整位图大小:

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = 50;
    int height = 50;  // image.getHeight()

    float bitmapRatio = (float) width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}
相关问题