Search code examples
androidimageodoo-11

take picture with Tablet it's rotated following the camera position


This app have the possibility to take picture and store it on tablet. I have some problem because this app use the Odoo framework and i don't know if it's a problem of that framework or something that i miss on Android code.

The problem is that when i take a picture, the image is good only if i take it with tablet landscape and with home button to the right. If i change rotation and take picture, the image is rotated... For example if i take picture with landscape orientation and home button to the left the image is upside down... If i take it portrait the image is rotated by 90°....

The Android request image don't manage rotation of image related to rotation of the device? Because if yes...it's a problem related to the odoo framework...if not i need to manage it.

EDIT___

We resize the image and use the Base64 for store and synchronize data with odoo when the phone is offline too.

onActivityResult()

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "onActivityResult() method");

    OValues response = fileManager.handleResult(requestCode, resultCode, data);
    if (response != null) {
        projectIssueImage = new ProjectIssueImage(this, null);
        OValues values = new OValues();

        String imageBase64 = BitmapUtils.uriToBase64(Uri.parse(response.getString("file_uri")), getContentResolver(), false);
        byte[] decodedImage = Base64.decode(imageBase64, Base64.DEFAULT);
        Bitmap bitmapImage = BitmapFactory.decodeByteArray(decodedImage, 0, decodedImage.length);
        Bitmap scaledBitmap = scaleDown(bitmapImage, 1000, true);
        ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOS);

        values.put("image", Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT));
        values.put("file_name", response.get("datas_fname"));
        values.put("file_type", response.get("file_type"));
        values.put("issue_id", currentObject);
        int _id = projectIssueImage.insert(values);
    }
    Intent returnIntent = new Intent();
    returnIntent.putExtra("id", currentObject);
    setResult(Activity.RESULT_OK, returnIntent);
    finish();
}

Code that resize image

public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
                               boolean filter) {
    float ratio = Math.min(
            (float) maxImageSize / realImage.getWidth(),
            (float) maxImageSize / realImage.getHeight());
    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, height, filter);

    return newBitmap;
}

Solution

  • Check this if it helps.

    public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
            int rotate = 0;
            try {
                context.getContentResolver().notifyChange(imageUri, null);
                File imageFile = new File(imagePath);
    
                ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
                }
    
                Log.i("RotateImage", "Exif orientation: " + orientation);
                Log.i("RotateImage", "Rotate value: " + rotate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rotate;
        }