0

Hi i have followed android developer site and tried to implement capture an image from camera programatically. I am able to capture the image and set it in the ImageView.
But when I am setting the image into the ImageView i am getting an image of less width and height. Instead i want the image captured from gallery or camera should fit into the ImageView element layout.

enter image description here

MyXML file is like as follows:

 <ImageView
            android:id="@+id/cmp_camera"
            android:layout_height="200dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_below="@+id/cmp_title"
            android:onClick="openCameraDialog"
            />

Since i have given the width as match_parent which is full screen I am getting less width. My requirement is like it should fit the ImageView layout. For camera coding I have followed this url:
http://developer.android.com/training/camera/photobasics.html

anand
  • 1,711
  • 2
  • 24
  • 59
  • 1
    use `scaleType="fitXY"` – Piyush Sep 26 '14 at 09:58
  • 1
    image1.setScaleType(ScaleType.FIT_XY); – iffu Sep 26 '14 at 10:06
  • possible duplicate of [Camera display / preview in full screen does not maintain aspect ratio - image is skewed, stretched in order to fit on the screen](http://stackoverflow.com/questions/16727836/camera-display-preview-in-full-screen-does-not-maintain-aspect-ratio-image-i) – Maveňツ Sep 26 '14 at 10:09

4 Answers4

2

Maybe you can try imgview.setScaleType(ScaleType.FIT_XY);

From XML, use this syntax: android:scaleType="fitXY".

The image is scaled using Matrix.ScaleToFit FILL, which performs the following:

Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.

See Android Documentation.

erad
  • 1,766
  • 2
  • 17
  • 26
2

Try this,

 android:scaleType="fitXY"

to your imageview inside xml file.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Jatin
  • 1,650
  • 3
  • 17
  • 32
1
ImageView.setImageBitmap(bitmap);


<ImageView
        android:id="@+id/cmp_camera"
        android:layout_height="200dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_below="@+id/cmp_title"
        android:onClick="openCameraDialog"
        android:scaleType="fitXY"
        />
Vaishali Sutariya
  • 5,093
  • 30
  • 32
0

Use this method to fit ur image and get rounded corner also

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 19;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

And Set image to ImageView

 addimg.setImageBitmap(getRoundedCornerBitmap(docode("Give path of your image here")));

Save image in sd card or phone memory, Decode it using its path.

 public static Bitmap decodeFile(File f) {
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
        Log.e("decodeFile", "" + e);
    }

    return null;
}
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38