Skip to main content

How to clear the orientation issue on Camera Image


We will be taking a picture with this library and create a Bitmap and set it in an ImageView as follow

public void onPictureTaken(CameraView cameraView, byte[] data) {
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
     imageView.setImageBitmap(bitmap);
}


This will work fine on most of the devices however there are some exceptional devices too, where the picture orientation appears to be incorrectly rotated 90 degrees to the left when the device orientations is portrait.



Solution to overcome this issue in your application

 Trick : Check if the width > height of the image then rotate by 90 

Follow the steps:

private static int fixOrientation(Bitmap bitmap) {
        if (bitmap.getWidth() > bitmap.getHeight()) {
            return 90;
        }
        return 0;
    }


Call this method to apply the rotation if needed


public static Bitmap flipIMage(Bitmap bitmap) {
        //Moustafa: fix issue of image reflection due to front camera settings
        Matrix matrix = new Matrix();
        int rotation = fixOrientation(bitmap);
        matrix.postRotate(rotation);
        matrix.preScale(-1, 1);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }


NOTE :  Scale by (-1,1) is because I use front camera and need to fix the reflection issue of camera.


Implement in your coding:

public void onPictureTaken(CameraView cameraView, byte[] data) {
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
     int oreo = fixOrientation(bitmap);

if(oreo==90){
    bitmap = flipIMage(thumbnail, oreo);
    imageView.setImageBitmap(bitmap);
}else{
    bitmap = flipIMage(thumbnail, oreo);
    imageView.setImageBitmap(bitmap);
}
     imageView.setImageBitmap(bitmap);
}

private static int fixOrientation(Bitmap bitmap) {
    if (bitmap.getWidth() > bitmap.getHeight()) {
        return 90;
    }
    return 0;
}
public static Bitmap flipIMage(Bitmap bitmap, int moreo) {
    //Moustafa: fix issue of image reflection due to front camera settings
    Matrix matrix = new Matrix();
    Bitmap bitReturn = null;
    if(moreo==90){
        int rotation = fixOrientation(bitmap);
        matrix.postRotate(rotation);
        matrix.preScale(-1, 1);
        bitReturn =  Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }else{
        matrix.preScale(-1, 1);
        bitReturn =  Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    return bitReturn;
}


Approach 2:


if(file!=null){ imageStoragePath = file.getAbsolutePath(); thumbnail = optimizeBitmap(BITMAP_SAMPLE_SIZE, imageStoragePath); try { ExifInterface ei = new ExifInterface(imageStoragePath); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); Bitmap rotatedBitmap = null; switch(orientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotatedBitmap = rotateImage(thumbnail, 90); break; case ExifInterface.ORIENTATION_ROTATE_180: rotatedBitmap = rotateImage(thumbnail, 180); break; case ExifInterface.ORIENTATION_ROTATE_270: rotatedBitmap = rotateImage(thumbnail, 270); break; case ExifInterface.ORIENTATION_NORMAL: default: rotatedBitmap = thumbnail; } mCameraPreview.setImageBitmap(rotatedBitmap); } catch (IOException e) { e.printStackTrace(); } Bitmap screenshot = getScreenShot(screenshotlay); store(screenshot,"lifecam.png");}

__________________________________________________________________________

Happy Coding...








Comments

Popular posts from this blog

Zoom Image - Android Tutorial

Here we are going to see how to zoom an image in Imageview Will see it through a sample 1. Create xml with an ImageView <? xml version="1.0" encoding="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout       xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity" >     < LinearLayout         android:layout_width="200dp"         android:layout_height="200dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"    ...

Multi language support

This post will help you yo learn how to make Multi language supported app in android. Why Multi language? In order to targeting global audience, it will be beneficial if you make your app localized. While localizing, you should think about text, audio, currency, numbers and graphics depending upon the region or country. But in this tutorial language only covered. Note: Whenever you are making any android application, Always declare text you want to use in your application in strings.xml only. <string name="hello">Hello World!</string> How String Localization Works? By default android considers English as primary language and loads the string resources from res >> values >> strings.xml . When you want to make Multilanguage supported app, you need to create a values folder by appending a Hyphen (-) and the ISO language code. For example for Hindi, values-hi named folder should be created and keep a strings.xml file in it wit...

Add custom font in Android using Calligraphy library

Are you fed up of Custom Views to set fonts? Or traversing the ViewTree to find TextViews? Sometime we want some other font for our Android application then you can add custom font in Android using Calligraphy library . Dependency Include the dependency Download (.aar) dependencies { compile ‘uk.co.chrisjenx:calligraphy:2.2.0’ } Add Fonts Add your custom fonts to assets/ . All font definitions are relative to this path. On Assets you should right-click New Directory, call it "fonts". In the finder put the .ttf  or .otf  font files in there. Create Class Create a class that extends Application and write this code public class App extends Application { @Override public void onCreate() { super.onCreate(); CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("your font path") .setFontAttrId(...