【发布时间】:2013-05-29 23:57:18
【问题描述】:
我正在尝试获取相机的预览并在其上绘制一个矩形(当用户选择一个区域时我得到它的坐标)但是当我在 ImageView 中显示照片时它会被缩放(更大的尺寸)。进入细节:
1_ 我的主要布局是 LinearLayout,其中包含 FrameLayout,而 FrameLayout 又包含相机预览
2_ 在我的活动中,我有一个android.hardware.Camera 的实例,我已经将它与PictureCallback 实例相关联并实现了onPictureTaken 方法来获取位图:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inScaled = false;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
3_拍照后,用户选择一个区域,我用这些坐标绘制矩形:
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);//mCameraPreview is an instance of "CameraPreview extends SurfaceView implements SurfaceHolder.Callback"
preview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent me) {
/* read the coordinates x1, y1, x2, y2 */
//the commented code was already tested, same result
//Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
Bitmap tempBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
Canvas canvas = new Canvas(tempBitmap);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawLine(x1, y1, x2, y1, paint);
canvas.drawLine(x1, y1, x1, y2, paint);
canvas.drawLine(x1, y2, x2, y2, paint);
canvas.drawLine(x2, y1, x2, y2, paint);
ImageView imageView = new ImageView(CameraActivity.this);
imageView.setImageBitmap(tempBitmap);
//the commented code was already tested, same result
//imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
imageView.draw(canvas);
LayoutParams lp = new LayoutParams(tempBitmap.getWidth(), tempBitmap.getHeight());
imageView.setLayoutParams(lp);
preview.addView(imageView);
}
}
最后的结果是绘制了矩形,但是位图比原来大很多,你知道为什么吗?
提前致谢。
PS:我在构建tempBitmap时尝试了bitmap.getWidth()/2,图像几乎是原始大小,但矩形也缩小了。
编辑:我已经尝试使用 imageView.setAdjustViewBounds(true) 和 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER)(也使用 ScaleType.CENTER、CENTER_CROP、FIT_XY 和 MATRIX)。有人知道发生了什么吗???
【问题讨论】:
标签: android bitmap android-imageview android-canvas