【发布时间】:2016-11-14 13:33:06
【问题描述】:
我将从相机检索到的每一帧转换为位图,并将其显示在 ImageView 上。我想要的是,在 imageView 上显示的位图的确切大小不是 imageView 本身的大小。 在下面发布的代码中是我的尝试以及我如何尝试获取位图的宽度和高度。但是下面发布的代码似乎只是给出了 imageView 本身的尺寸,因为我对代码进行了两次测试,如下所示:
第一次测试:
我将 layout_weight 属性设置为 .3,如下所示
<RelativeLayout
android:id="@+id/mainActivity_imageView_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".3"
android:layout_below="@id/mainActivity_cameraPreview_container">
<ImageView
android:id="@+id/mainActivity_ivEdges"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
在运行时我收到以下输出:
11-14 14:16:16.782 585-681/com.example.textureview_03 D/PreviewSurface: before bitmap.getWidth(): 960
11-14 14:16:16.782 585-681/com.example.textureview_03 D/PreviewSurface: before bitmap.getHeight(): 720
11-14 14:16:16.907 585-585/com.example.textureview_03 D/PreviewSurface: after resizedBitmap.getWidth(): 720
11-14 14:16:16.907 585-585/com.example.textureview_03 D/PreviewSurface: after resizedBitmap.getHeight(): 960
正在显示的图像
第二次测试:
我将 layout_weight 属性设置为 .6,如下所示
<RelativeLayout
android:id="@+id/mainActivity_imageView_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".6"
android:layout_below="@id/mainActivity_cameraPreview_container">
<ImageView
android:id="@+id/mainActivity_ivEdges"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
在运行时我收到以下输出:
11-14 14:17:33.577 2241-2339/com.example.textureview_03 D/PreviewSurface: before bitmap.getWidth(): 960
11-14 14:17:33.577 2241-2339/com.example.textureview_03 D/PreviewSurface: before bitmap.getHeight(): 720
11-14 14:17:33.667 2241-2241/com.example.textureview_03 D/PreviewSurface: after resizedBitmap.getWidth(): 720
11-14 14:17:33.667 2241-2241/com.example.textureview_03 D/PreviewSurface: after resizedBitmap.getHeight(): 960
正在显示的图像
因此,我得出结论,我在 logcat 输出中得到的大小不是 imageView 上显示的位图的确切大小,但是,它可能是 ImageView 本身的大小
请告诉我如何获取在 imageView 上显示的位图的确切大小?
代码:
@Override
protected Void doInBackground(Void... params) {
Camera.Parameters parameters = mCamera.getParameters();
int format = parameters.getPreviewFormat();
//YUV formats require more conversion
if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
// Get the YuV image
YuvImage yuv_image = new YuvImage(mData, format, w, h, null);
Rect rect = new Rect(0, 0, w, h);
ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
yuv_image.compressToJpeg(rect, 100, output_stream);
byte[] byt = output_stream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(byt, 0, byt.length);
Log.d(TAG, "before bitmap.getWidth(): " + bitmap.getWidth());
Log.d(TAG, "before bitmap.getHeight(): " + bitmap.getHeight());
//required to rotate the bitmap, because it is initially lopsided
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
publishProgress(resizedBitmap);
}
@Override
protected void onProgressUpdate(Bitmap... values) {
super.onProgressUpdate(values);
Bitmap resizedBitmap = values[0];
Log.d(TAG, "after resizedBitmap.getWidth(): " + resizedBitmap.getWidth());
Log.d(TAG, "after resizedBitmap.getHeight(): " + resizedBitmap.getHeight());
mIVEdges.setImageBitmap(resizedBitmap);
}
【问题讨论】:
-
使用
layout_width&layout_weightwrap_content并尝试 -
@kirankumar 我试过了..无论 layout_weight 是多少,我都会得到相同的尺寸!!!
标签: android bitmap opengl-es imageview