1

我正在尝试获取相机的预览并在其上绘制一个矩形(当用户选择一个区域时我得到它的坐标)但是当我在 ImageView 中显示照片时它会被缩放(更大的尺寸)。进入细节:

1_我的主要布局是一个LinearLayout,它包含一个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)and imageView.setScaleType(ImageView.ScaleType.FIT_CENTER)(也使用 ScaleType.CENTER、CENTER_CROP、FIT_XY 和 MATRIX)。有谁知道发生了什么???

4

1 回答 1

0

默认情况下,图像视图在位图上应用比例以使位图适合图像视图的整个区域;

试试这个代码

ImageView imageView = new ImageView(CameraActivity.this);
imageView .setImageMatrix (new Matrix());
imageView .setScaleType(ImageView.ScaleType.MATRIX);
imageView.setImageBitmap(tempBitmap);

并从您的代码中删除这一行

imageView.draw(canvas);
于 2013-05-31T08:38:17.107 回答