0

I want to add image capturing in my Android app where user can captures images, I'm using the following code:

public void open_camera() { 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

    try {
        // place where to store camera taken picture
        photo = this.createTemporaryFile("picture", ".jpg");
        photo.delete();
    } catch(Exception e) {
        Toast.makeText(this, "Please check SD card! Image shot is impossible!", 10000);   
    }
    mImageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    //start camera intent
    startActivityForResult(intent,SELECT_PICTURE_CAMERA );

    //Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    //startActivityForResult(cameraIntent, SELECT_PICTURE_CAMERA); 
}

private File createTemporaryFile(String part, String ext) throws Exception {
    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
    if(!tempDir.exists()) {
        tempDir.mkdir();
    }
    return File.createTempFile(part, ext, tempDir);
}

When I retrieve

case SELECT_PICTURE_CAMERA:
    if(resultCode == RESULT_OK) {
        // Toast.makeText(this,"Camera" + imageReturnedIntent.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_LONG).show();
        //this.yourSelectedImage = (Bitmap) imageReturnedIntent.getExtras().get("data");
        /*
        ImageButton imgbtn = (ImageButton) findViewById(R.id.imageButton_Photo);
        BitmapDrawable background = new BitmapDrawable(this.yourSelectedImage);
        imgbtn.setBackgroundDrawable(background);
        */

        try {
            File f=new File(mImageUri.getPath());

            ExifInterface exif = new ExifInterface(f.getPath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            int angle = 0;
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                angle = 90;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                angle = 180;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                angle = 270;
            }

            Matrix mat = new Matrix();
            mat.postRotate(90);

            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
            this.yourSelectedImage = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);                 
        } catch (IOException e) {
            Log.w("TAG", "-- Error in setting image");
        } catch(OutOfMemoryError oom) {
            Log.w("TAG", "-- OOM Error in setting image");
        }

But unfortunately after doing all this, still image is not of full size and it's always landscape.

ssantos
  • 16,001
  • 7
  • 50
  • 70
Ali
  • 10,774
  • 10
  • 56
  • 83

1 Answers1

0

You have a small mistake in code, after you get the angle the image should rotate you need to use this value in postRotate.

Please replace the line: mat.postRotate(90);

with: mat.postRotate(angle);

Hope I helped you :-)

Regards, Woody.

Woody
  • 1,589
  • 14
  • 16