我正在将从相机中检索到的每个帧转换为位图,然后将其显示在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上显示的Bitmap的确切大小?
的码:
@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);
}
答案 0 :(得分:0)
也许这一行,
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
给你带来麻烦。使旋转矩阵90度正常,但resizedBitmap的宽度不再是宽度。试试这个:
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getHeight() , bitmap.getWidth() , matrix, true);
我希望这会对你有所帮助