How can handle image rotation problem after capture image from camera android


1. 1st you need to detect current orientation of the image based on below method, and the call rotateImage() with Rotation degree value .


String TAG="ImageRotate";

public static void ReRotateImage(String imgfilepath){
try {
             ei = new ExifInterface(imgfilepath);
             int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
             Log.e(TAG,"orientation: "+ orientation);
            // Crashlytics.setInt("PICTURE ORIENTAION",orientation);
             switch(orientation) {
             case ExifInterface.ORIENTATION_ROTATE_90:
                 //rotateImage(bitmap, 90);
                 Log.e(TAG,"ORIENTATION_ROTATE_90");
                 rotateImage(imgfilepath, 90);
                 break;
             case ExifInterface.ORIENTATION_ROTATE_180:
                // rotateImage(bitmap, 180);
                  Log.e(TAG,"ORIENTATION_ROTATE_180");
                 rotateImage(imgfilepath, 180);
                 break;
             case ExifInterface.ORIENTATION_ROTATE_270:
                    // rotateImage(bitmap, 180);
                      Log.e(TAG,"ORIENTATION_ROTATE_270");
                     rotateImage(imgfilepath, 270);
                     break;
             // etc.
                 default:
                     Log.e(TAG, "ORIENTATION_ROTATE_0");
                    // rotateImage1(imgfilepath, 0);
                     break;
         }
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
}


2. Method image rotated as per demand


private static Bitmap rotateImage(String strImagePath, int degree) {

       // Bitmap img = BitmapFactory.decodeFile(strImagePath);
       // Bitmap img=convertBitmapNew(strImagePath);//convertBitmap(strImagePath);

        Bitmap img=BitmapHelper.decodeFile(new File(strImagePath),1024,1024,true);

        /*NEW CODE*/
        if ((img.getWidth() >= 1024) && (img.getHeight() >= 1024)) {
    BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
    bmpOptions.inSampleSize = 1;
    while ((img.getWidth() >= 1024) && (img.getHeight() >= 1024)) {
        bmpOptions.inSampleSize++;
        img = BitmapFactory.decodeFile(strImagePath, bmpOptions);
    }
    LogUtils.showDebugLog(TAG+"camera", "Resize1: " + bmpOptions.inSampleSize);
}
        /*--------*/
       
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
       
        try {
            FileOutputStream bmpFile = new FileOutputStream(strImagePath);
            rotatedImg.compress(Bitmap.CompressFormat.JPEG, 100, bmpFile);
            bmpFile.flush();
            bmpFile.close();
        } catch (Exception e) {
            LogUtils.showDebugLog(TAG+"camera", "Error on saving file");
        }
        img.recycle();
        return rotatedImg;
    }

3. Convert Bit map


 public static Bitmap convertBitmap(String path)   {
        LogUtils.showDebugLog(TAG+"camera", "Entry Rotate: ");
        Bitmap bitmap=null;

        BitmapFactory.Options bfOptions=new BitmapFactory.Options();
       // bfOptions.inDither=false;                     //Disable Dithering mode
        bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        bfOptions.inTempStorage=new byte[32 * 1024];

        /*New code 20/04/16*/
        bfOptions.inJustDecodeBounds = false;
        bfOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        bfOptions.inDither = true;
        /******************/


        File file=new File(path);
        FileInputStream fs=null;
        try {
            fs = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            if(fs!=null)
            {
                bitmap= BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
            }
        } catch (IOException e) {

            e.printStackTrace();
        } finally{
            if(fs!=null) {
                try {
                    fs.close();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
        }

        return bitmap;
    }


    public static Bitmap convertBitmapNew(String path){
        Bitmap bitmap=null;
        /******************************/
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path,options);
        options.inSampleSize = calculateInSampleSize(options, 1024, 1024);
        options.inJustDecodeBounds = false;

        bitmap= BitmapFactory.decodeFile(path,options);
        /******************/
        return bitmap;
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        LogUtils.showDebugLog(TAG,""+inSampleSize);

        return inSampleSize;
    }


this is you can call from a class as per i declared




No comments:

IRCTC Share Price Declines by 2% Despite 30% Jump in Q4 Net Profit; Board Announces Dividend of INR 2 per Share

Introduction: The share price of Indian Railway Catering and Tourism Corporation (IRCTC) experienced a decline of 2% in today's trading ...