Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera orientation in samsung device issue

I am successfully clicking the image and saving it in the card. It is working perfectly in all the devices, but issue is that when I am testing it in Samsung device, then as I am clicking image in the portrait mode then it is saving image by default in the landscape mode. But I am not doing any type of rotating code. So please help me. How can I solve it??

private void takePicture() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    try {
        Uri mImageCaptureUri = null;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mImageCaptureUri = Uri.fromFile(mFileTemp);
        } else {
            mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
        }
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                mImageCaptureUri);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
    } catch (ActivityNotFoundException e) {

        Log.d("", "cannot take picture", e);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK) {

        return;
    }

    Bitmap bitmap;

    switch (requestCode) {

    case REQUEST_CODE_TAKE_PICTURE:

        startCropImage();
        break;
    case REQUEST_CODE_CROP_IMAGE:

        String path = data.getStringExtra(CropImage.IMAGE_PATH);
        if (path == null) {

            return;
        }

        bitmap = BitmapFactory.decodeFile(mFileTemp.getPath());
        resizedBitmap = ScalingUtilities.createScaledBitmap(bitmap, 320,
                320, ScalingLogic.CROP);
        imgPreview.setImageBitmap(resizedBitmap);
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


private void startCropImage() {

    Intent intent = new Intent(this, CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
    intent.putExtra(CropImage.SCALE, true);

    intent.putExtra(CropImage.ASPECT_X, 2);
    intent.putExtra(CropImage.ASPECT_Y, 2);

    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}

I added following permissions in the manifest.

   <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 430
Sachin Avatar asked Aug 09 '14 09:08

Sachin


People also ask

How do I fix the camera rotation on my Samsung?

Swipe down from the top of the screen to open the Quick settings panel. Tap More options (the three vertical dots), and then tap Edit buttons. Touch and hold the Auto rotate icon, and then drag it to your desired position.

How do I fix my camera rotation on my Android?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

How do I fix portrait orientation?

For Android users: Swipe your finger down from the top of the screen to bring up your quick settings toolbar. You will have an option for rotation that can be tapped to turn off the rotation lock.


2 Answers

I solved my bug, actually I use one trick. I took the manufactured name programtically, like this

 String device_name = Build.MANUFACTURER;

After that I saved it in the sharedpreferences object.

 deviceIdPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
    SharedPreferences.Editor   editor = deviceIdPreferences.edit();
    editor.putString("Device_id",deviceId);
    editor.putString("device_name",device_name);
    editor.commit();

After that in the destination java file, I extrat the value from sharedpreference.

preferences = this.getSharedPreferences(
            "USER_INFO", Context.MODE_PRIVATE);
    device_name = preferences.getString("device_name", "Empty");

and then

    mBitmap = getBitmap(mImagePath);
    if(device_name.equals("samsung")){

            switch (Integer.parseInt(gotOrientation)) {

                case ExifInterface.ORIENTATION_ROTATE_90:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
                default:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
            }

        }




 private Bitmap getBitmap(String path) {

    Uri uri = getImageUri(path);
    InputStream in = null;
    try {
        in = mContentResolver.openInputStream(uri);

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(in, null, o);
        in.close();

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        in = mContentResolver.openInputStream(uri);
        Bitmap b = BitmapFactory.decodeStream(in, null, o2);
        in.close();

        return b;
    } catch (FileNotFoundException e) {
        Log.e(TAG, "file " + path + " not found");
    } catch (IOException e) {
        Log.e(TAG, "file " + path + " not found");
    }
    return null;
}
like image 168
Sachin Avatar answered Sep 30 '22 02:09

Sachin


Many devices -- but not all -- will not save portrait images. They will save landscape images with an EXIF header telling the image viewer to rotate the image to portrait. Not all image viewers handle this.

This is part of the reason I wrote the CWAC-Camera library, to provide uniform camera output from different devices.

The code that I use for this is reminiscent of what Haresh points out in his comment, with some noteworthy differences:

  1. Haresh's code appears to assume that the camera was in portrait mode; mine aims to handle either case.

  2. Haresh's code does all the hard work explicitly on the main application thread, including the disk I/O, which is a very bad idea. Mine is a background thread.

  3. His code downsamples the image. My guess is that this is to help prevent out-of-memory errors, which is definitely a challenge here. In my case, I give the developers using my library the option of specifying a large heap, to help ensure that I can perform the bitmap rotation. Eventually, I will move this code to the NDK, where memory allocations are not counted against the Dalvik heap limit.

  4. My code is also responsible for other device-specific issues, such as flipping the FFC image if needed or mirroring the FFC image if requested by the developer.

However, my code is specifically designed to be used by my library, and so it would need a fair amount of work to break out the logic into something that could be used independently.

Note that the link to my code is specifically to v0.6.9 of the library. Newer versions of this class may exist in the repo if you are reading this months after I wrote this answer. Conversely, the class may no longer exist in the repo master, though, if I have moved the code into the NDK.

like image 30
CommonsWare Avatar answered Sep 30 '22 01:09

CommonsWare