如何在 ImageView 显示 Bitmap 后获取其尺寸大小

8

我有一个ImageView

<ImageView
        android:id="@+id/imgCaptured"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/captured_image" />

我从摄像头中捕获了一张图像,将该图像转换为位图。
Bitmap thumbnail;
thumbnail = MediaStore.Images.Media.getBitmap(getActivity()
                    .getContentResolver(), imageUri);

当我在以上ImageView中显示此位图之前,获取其分辨率的方法是:
Log.i("ImageWidth = " + thumbnail.getWidth(), "ImageHeight = "
                + thumbnail.getHeight());

返回结果是ImageWidth = 2592 ImageHeight = 1936

然后我用imgCaptured.setImageBitmap(thumbnail);在我的上面的imageview中显示这个位图,然后我获取了我的imageview的大小。

Log.i("ImageView Width = " + imgCaptured.getWidth(),
                "ImageView Height = " + imgCaptured.getHeight());

这让我知道了 ImageView 的宽度为480,高度为720。

现在我的问题是:

  • How can i get the size of that bitmap after displaying it in my imageview. I know this can be done by using this

    image.buildDrawingCache();
    Bitmap bmap = image.getDrawingCache();
    

    but this will create a new bitmap of size equal to that of imageview.

  • I also want to know that, Does image resized automatically after displaying it in the imageview. if yes then is there any way to display the image in imageview without resizing the image.

编辑

我实际上拍摄了一张2592x1936的图像,并在我的imageView中显示了这张图像,对这张图像进行了一些其他操作。现在我想以同样的2592x1936分辨率保存这个图像。这可能吗?

提前致谢。


澄清一下 - 你的意思是 ImageView 中位图的大小吗?如果没有填充,两者是等价的,ImageView 的宽度/高度就是位图的宽度/高度。如果有填充,只需将其减去即可。 - Gabe Sechan
是的,我指的是在ImageView中显示后位图的大小。 - Qadir Hussain
@Gabe Sechan,实际上我已经捕获了一张2592x1936的图像。我在我的imageView中显示了这张图片,并对这张图片进行了一些其他操作。现在我想以相同的2592x1936分辨率保存这张图片。这是否可能? - Qadir Hussain
是的,但可能不是完全质量。如果你缩小了图像(没有保留完整尺寸版本),那么在缩小它时会丢失数据,并且当Android扩展它时,Android将猜测数据是什么。如果您操作了完整尺寸版本,则非常容易-只需使用位图类上的compress()函数和FileOutputStream即可。 - Gabe Sechan
谢谢回复。您能否贴上一段代码,用于从具有2592x1936大小的ImageView中检索全尺寸图像。 - Qadir Hussain
1个回答

13
在你展示一个Bitmap在一个ImageView中之后,ImageView会创建一个BitmapDrawable对象来在ImageView的画布中绘制它。因此,你可以调用ImageView.getDrawable()方法来获取BitmapDrawable的引用,并通过调用Drawable.getBounds(Rect rect)方法来获取边界。通过这些边界,你可以计算出在ImageView中绘制的Bitmap的宽度和高度。
Drawable drawable = ImageView.getDrawable();
//you should call after the bitmap drawn
Rect bounds = drawable.getBounds();
int width = bounds.width();
int height = bounds.height();
int bitmapWidth = drawable.getIntrinsicWidth(); //this is the bitmap's width
int bitmapHeight = drawable.getIntrinsicHeight(); //this is the bitmap's height

出现了这个错误。类型Drawable中的方法getBounds()不适用于参数(Rect)。 - Qadir Hussain
1
我为我的ImageView设置了scaleType="fitXY"。我将其删除后,现在它正常工作了。非常感谢。 - Qadir Hussain
请问如何以2592x1936的原始分辨率保存这张图片? - Qadir Hussain
1
就像 Quadir 所说的那样,你应该小心使用 "scaleType" 属性。因为它会改变 ImageView 的高度。 - oguzhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接