From 910f03f2ee057bcc97e169d7d74b3dcfea9b8925 Mon Sep 17 00:00:00 2001 From: RoyleAndroidDev Date: Mon, 25 Dec 2017 13:58:14 +0700 Subject: [PATCH] Fix bug rotation image. (preview image rotate incorrect) & Update version gradle. --- ScanDemoExample/app/build.gradle | 8 +- .../java/com/scanner/demo/MainActivity.java | 4 +- ScanDemoExample/build.gradle | 4 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- ScanDemoExample/scanlibrary/build.gradle | 2 +- ScanDemoExample/scanlibrary/scanlibrary.iml | 83 ++++++++++++------- .../com/scanlibrary/PickImageFragment.java | 31 ++++++- 7 files changed, 95 insertions(+), 39 deletions(-) diff --git a/ScanDemoExample/app/build.gradle b/ScanDemoExample/app/build.gradle index 3060efc0..a5297a5c 100644 --- a/ScanDemoExample/app/build.gradle +++ b/ScanDemoExample/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 21 - buildToolsVersion "21.1.2" + compileSdkVersion 26 + buildToolsVersion "26.0.2" defaultConfig { applicationId "com.scanner.demo" minSdkVersion 14 - targetSdkVersion 21 + targetSdkVersion 26 versionCode 1 versionName "1.0" } @@ -22,5 +22,5 @@ android { dependencies { compile project(':scanlibrary') compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:21.0.3' + compile 'com.android.support:appcompat-v7:26.1.0' } diff --git a/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java b/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java index a45db24c..ca4d9e06 100644 --- a/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java +++ b/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java @@ -7,7 +7,7 @@ import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; -import android.support.v7.app.ActionBarActivity; +import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -20,7 +20,7 @@ import java.io.IOException; -public class MainActivity extends ActionBarActivity { +public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE = 99; private Button scanButton; diff --git a/ScanDemoExample/build.gradle b/ScanDemoExample/build.gradle index 6356aabd..dcba93da 100644 --- a/ScanDemoExample/build.gradle +++ b/ScanDemoExample/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:1.0.0' + classpath 'com.android.tools.build:gradle:3.0.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,5 +16,6 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/ScanDemoExample/gradle/wrapper/gradle-wrapper.properties b/ScanDemoExample/gradle/wrapper/gradle-wrapper.properties index 0c71e760..649480dd 100644 --- a/ScanDemoExample/gradle/wrapper/gradle-wrapper.properties +++ b/ScanDemoExample/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip diff --git a/ScanDemoExample/scanlibrary/build.gradle b/ScanDemoExample/scanlibrary/build.gradle index f47ef062..a90e4e46 100644 --- a/ScanDemoExample/scanlibrary/build.gradle +++ b/ScanDemoExample/scanlibrary/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' android { compileSdkVersion 24 - buildToolsVersion "25.0.0" + buildToolsVersion "26.0.2" defaultConfig { minSdkVersion 14 diff --git a/ScanDemoExample/scanlibrary/scanlibrary.iml b/ScanDemoExample/scanlibrary/scanlibrary.iml index bb6b1082..ade0247c 100644 --- a/ScanDemoExample/scanlibrary/scanlibrary.iml +++ b/ScanDemoExample/scanlibrary/scanlibrary.iml @@ -1,5 +1,5 @@ - + @@ -11,78 +11,103 @@ - + + + - - - - - - - + + + + + + + + + - + + + + + + + + + + + + + + + + - - + + + + + + + + + + + - - - - - - - + + + - - - + + + - + + + - - + \ No newline at end of file diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PickImageFragment.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PickImageFragment.java index 8ef6963c..bd5aa5b0 100644 --- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PickImageFragment.java +++ b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PickImageFragment.java @@ -6,6 +6,8 @@ import android.content.res.AssetFileDescriptor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -177,9 +179,36 @@ private Bitmap getBitmap(Uri selectedimg) throws IOException { AssetFileDescriptor fileDescriptor = null; fileDescriptor = getActivity().getContentResolver().openAssetFileDescriptor(selectedimg, "r"); + + Matrix matrix = new Matrix(); + + ExifInterface exif = null; //Since API Level 5 + try { + exif = new ExifInterface(selectedimg.getPath()); + int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + int rotationInDegrees = exifToDegrees(rotation); + if (rotation != 0f) { + matrix.preRotate(rotationInDegrees); + } + } catch (IOException e) { + e.printStackTrace(); + } + Bitmap original = BitmapFactory.decodeFileDescriptor( fileDescriptor.getFileDescriptor(), null, options); - return original; + + return Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true); + } + + private static int exifToDegrees(int exifOrientation) { + if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { + return 90; + } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { + return 180; + } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { + return 270; + } + return 0; } } \ No newline at end of file