Bitmap scaled after drawing with canvas

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Bitmap scaled after drawing with canvas

Postby maxivis » Tue Jun 04, 2013 12:49 am

Hi guys I'm trying to take a camera's preview and draw a rectangle over it (I get its coordinates when the user selects an area) but when I show the photo in an ImageView it gets scaled (bigger size). Going to the detail:

1_ My main layout is a LinearLayout which contains a FrameLayout which in turn will contain the camera preview
2_ In my activity I have an instance of android.hardware.Camera and I've associated it a PictureCallback instance and implemented the onPictureTaken method to get the bitmap:

Code: Select all
BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inPurgeable = true;           
        opts.inInputShareable = true;
        opts.inScaled = false;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);

3_After taking the photo the user selects an area and I take those coordinates for drawing the rectangle:
Code: Select all
preview = (FrameLayout) findViewById(;
preview.addView(mCameraPreview);//mCameraPreview is an instance of "CameraPreview extends SurfaceView implements SurfaceHolder.Callback"
preview.setOnTouchListener(new OnTouchListener() {
    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();
    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);
    //the commented code was already tested, same result   
    //imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
    LayoutParams lp = new LayoutParams(tempBitmap.getWidth(), tempBitmap.getHeight());

The final result is that the rectangle is drawn, but the bitmap is much bigger than the original, do you know why? Thanks in advance.
PS: I tried bitmap.getWidth()/2 when building the tempBitmap and the image was almost the original size, but the rectangle also shrink. Currently I'm trying with imageView.setAdjustViewBounds(true) and imageView.setScaleType(ImageView.ScaleType.FIT_CENTER) (also with ScaleType.CENTER, CENTER_CROP, FIT_XY and MATRIX) but doesn't seems to work.
Thanks in advance
Posts: 3
Joined: Mon Dec 05, 2011 4:09 pm


Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 10 guests