Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
feat: android rotate image when orientation is not normal
Browse files Browse the repository at this point in the history
  • Loading branch information
killi8n committed Jan 4, 2021
1 parent 09e828e commit 61f225b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion android/reactnativeimageselector.iml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</compilerSettings>
<compilerArguments>
<option name="destination" value="$MODULE_DIR$/build/tmp/kotlin-classes/debug" />
<option name="classpath" value="$USER_HOME$/.gradle/caches/transforms-2/files-2.1/f33d2123a23918c5c321570d3632c27f/jetified-react-native-0.62.2-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.infer.annotation/infer-annotation/0.11.2/f514ff4ca022a579d9cf7524846988b646ae4491/infer-annotation-0.11.2.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.yoga/proguard-annotations/1.14.1/3d015bb821875657ac8e4b808a223aae339defb2/proguard-annotations-1.14.1.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/javax.inject/javax.inject/1/6975da39a7040257bd51d21a231b76c915872d38/javax.inject-1.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/d0356cfa50447fb793907fae1eefb5b4/constraintlayout-2.0.1-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/dd806a99f08184d0856dc1e7f2dbc503/appcompat-1.2.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/6e9d20126a8a118d30ab52453e17d2b3/fragment-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/0cb90c9e0e1179eb19f80dd3a9310e3c/appcompat-resources-1.2.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/3ccf16ae0a8db322028a1c70c792f84e/drawerlayout-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/bba3d90fa0c2260f06382ad16d659a72/viewpager-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/8992271cc1986c5a5dd8b58ea6a580d2/loader-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/862c1ad2404302414565c7a6d6b2d99f/activity-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/d2498433c6ff90c97c0444f4f746b598/vectordrawable-animated-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/f30b69a00566de67c1e89201d40c5c10/vectordrawable-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/0199edb1aa05807f59e8d3210dfbe5ca/customview-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/d094a4f7aaaa4c0eb9cd8a93dd5e2917/core-1.3.1-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/41f8fa99fe5fb6a6f13fb23c59f783a2/cursoradapter-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/9ef3f1b043b98e77e27af3746a6817d5/lifecycle-runtime-2.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/35dfb4bc226af74943a0f8bcc1d9131f/versionedparcelable-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.collection/collection/1.1.0/1f27220b47669781457de0d600849a5de0e89909/collection-1.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/7881596bdd00b350683a9742baaf6a7a/lifecycle-viewmodel-2.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/a5e59e30523dd75bbaeaf02048445143/savedstate-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/10780035ece67258beb95cd9083db991/lifecycle-livedata-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/6f7f370ab5efbff67ffedf3d38a47011/lifecycle-livedata-core-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.lifecycle/lifecycle-common/2.1.0/c67e7807d9cd6c329b9d0218b2ec4e505dd340b7/lifecycle-common-2.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/21604c423a565d29b5ce02b20a59f559/core-runtime-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.arch.core/core-common/2.1.0/b3152fc64428c9354344bd89848ecddc09b6f07e/core-common-2.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/9cebdcd941854b68f1b31a5f798ac266/interpolator-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.annotation/annotation/1.1.0/e3a6fb2f40e3a3842e6b7472628ba4ce416ea4c8/annotation-1.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/e3b48dcc2c2d9a4366d14ce59e4a9a7c/fresco-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/74f72539e53d61eabf0d89201b08b33f/jetified-fbcore-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/e26159f319ae619bd4a54ab9ee8d0f33/drawee-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/caac8622bb50967db5e1ebd0f5054bc9/imagepipeline-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/381bde14d71b2f3f231a03e013baee28/imagepipeline-base-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/a17038308017472677144c4b2591e0a7/soloader-0.8.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/8d7572e5d2c6dd9ac45103ec78b59094/annotation-0.8.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.soloader/nativeloader/0.8.0/50524ca901bccb0540204b8166abb23557809050/nativeloader-0.8.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/99a5b1fb3871c82838963a00cdbe1c4c/nativeimagefilters-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/95b32e0f56cdc7359226446d6c6ac960/nativeimagetranscoder-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/cc4d1b9170966ad97d28c85edca70c7d/imagepipeline-okhttp3-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.squareup.okhttp3/okhttp-urlconnection/3.12.1/f689d3657c3f1fdbde4877bcddc668491a4ad46f/okhttp-urlconnection-3.12.1.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.squareup.okhttp3/okhttp/3.12.1/dc6d02e4e68514eff5631963e28ca7742ac69efe/okhttp-3.12.1.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.squareup.okio/okio/1.15.0/bc28b5a964c8f5721eb58ee3f3c47a9bcbf4f4d8/okio-1.15.0.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.fbjni/fbjni-java-only/0.0.3/c4540aecb99b9ec380acef6c10bb6f700de8ac2c/fbjni-java-only-0.0.3.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-android-extensions-runtime/1.3.50/bec16087637a7cafe54894e73d38037977cb30d2/kotlin-android-extensions-runtime-1.3.50.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.3.50/b529d1738c7e98bbfa36a4134039528f2ce78ebf/kotlin-stdlib-1.3.50.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.3.50/3d9cd3e1bc7b92e95f43d45be3bfbcf38e36ab87/kotlin-stdlib-common-1.3.50.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.constraintlayout/constraintlayout-solver/2.0.1/30988fe2d77f3fe3bf7551bb8a8b795fad7e7226/constraintlayout-solver-2.0.1.jar:/Users/donghochoi/react-native-image-selector/android/build/intermediates/compile_only_not_namespaced_r_class_jar/debug/R.jar:/Users/donghochoi/Library/Android/sdk/platforms/android-28/android.jar:/Users/donghochoi/Library/Android/sdk/build-tools/28.0.3/core-lambda-stubs.jar" />
<option name="classpath" value="$USER_HOME$/.gradle/caches/transforms-2/files-2.1/30b09b734e3520dae209a4f65ad050e2/jetified-react-native-0.62.2-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.infer.annotation/infer-annotation/0.11.2/f514ff4ca022a579d9cf7524846988b646ae4491/infer-annotation-0.11.2.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.yoga/proguard-annotations/1.14.1/3d015bb821875657ac8e4b808a223aae339defb2/proguard-annotations-1.14.1.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/javax.inject/javax.inject/1/6975da39a7040257bd51d21a231b76c915872d38/javax.inject-1.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/d0356cfa50447fb793907fae1eefb5b4/constraintlayout-2.0.1-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/dd806a99f08184d0856dc1e7f2dbc503/appcompat-1.2.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/6e9d20126a8a118d30ab52453e17d2b3/fragment-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/0cb90c9e0e1179eb19f80dd3a9310e3c/appcompat-resources-1.2.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/3ccf16ae0a8db322028a1c70c792f84e/drawerlayout-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/bba3d90fa0c2260f06382ad16d659a72/viewpager-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/8992271cc1986c5a5dd8b58ea6a580d2/loader-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/862c1ad2404302414565c7a6d6b2d99f/activity-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/d2498433c6ff90c97c0444f4f746b598/vectordrawable-animated-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/f30b69a00566de67c1e89201d40c5c10/vectordrawable-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/0199edb1aa05807f59e8d3210dfbe5ca/customview-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/d094a4f7aaaa4c0eb9cd8a93dd5e2917/core-1.3.1-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/41f8fa99fe5fb6a6f13fb23c59f783a2/cursoradapter-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/9ef3f1b043b98e77e27af3746a6817d5/lifecycle-runtime-2.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/35dfb4bc226af74943a0f8bcc1d9131f/versionedparcelable-1.1.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.collection/collection/1.1.0/1f27220b47669781457de0d600849a5de0e89909/collection-1.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/7881596bdd00b350683a9742baaf6a7a/lifecycle-viewmodel-2.1.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/a5e59e30523dd75bbaeaf02048445143/savedstate-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/10780035ece67258beb95cd9083db991/lifecycle-livedata-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/6f7f370ab5efbff67ffedf3d38a47011/lifecycle-livedata-core-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.lifecycle/lifecycle-common/2.1.0/c67e7807d9cd6c329b9d0218b2ec4e505dd340b7/lifecycle-common-2.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/21604c423a565d29b5ce02b20a59f559/core-runtime-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.arch.core/core-common/2.1.0/b3152fc64428c9354344bd89848ecddc09b6f07e/core-common-2.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/9cebdcd941854b68f1b31a5f798ac266/interpolator-1.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.annotation/annotation/1.1.0/e3a6fb2f40e3a3842e6b7472628ba4ce416ea4c8/annotation-1.1.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/e3b48dcc2c2d9a4366d14ce59e4a9a7c/fresco-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/f44da6e7d8473cdcbcc1268968a311ee/jetified-fbcore-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/e26159f319ae619bd4a54ab9ee8d0f33/drawee-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/caac8622bb50967db5e1ebd0f5054bc9/imagepipeline-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/381bde14d71b2f3f231a03e013baee28/imagepipeline-base-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/a17038308017472677144c4b2591e0a7/soloader-0.8.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/8d7572e5d2c6dd9ac45103ec78b59094/annotation-0.8.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.soloader/nativeloader/0.8.0/50524ca901bccb0540204b8166abb23557809050/nativeloader-0.8.0.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/99a5b1fb3871c82838963a00cdbe1c4c/nativeimagefilters-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/95b32e0f56cdc7359226446d6c6ac960/nativeimagetranscoder-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/transforms-2/files-2.1/cc4d1b9170966ad97d28c85edca70c7d/imagepipeline-okhttp3-2.0.0-api.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.squareup.okhttp3/okhttp-urlconnection/3.12.1/f689d3657c3f1fdbde4877bcddc668491a4ad46f/okhttp-urlconnection-3.12.1.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.squareup.okhttp3/okhttp/3.12.1/dc6d02e4e68514eff5631963e28ca7742ac69efe/okhttp-3.12.1.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.squareup.okio/okio/1.15.0/bc28b5a964c8f5721eb58ee3f3c47a9bcbf4f4d8/okio-1.15.0.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/com.facebook.fbjni/fbjni-java-only/0.0.3/c4540aecb99b9ec380acef6c10bb6f700de8ac2c/fbjni-java-only-0.0.3.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-android-extensions-runtime/1.3.50/bec16087637a7cafe54894e73d38037977cb30d2/kotlin-android-extensions-runtime-1.3.50.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.3.50/b529d1738c7e98bbfa36a4134039528f2ce78ebf/kotlin-stdlib-1.3.50.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.3.50/3d9cd3e1bc7b92e95f43d45be3bfbcf38e36ab87/kotlin-stdlib-common-1.3.50.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar:/Users/donghochoi/.gradle/caches/modules-2/files-2.1/androidx.constraintlayout/constraintlayout-solver/2.0.1/30988fe2d77f3fe3bf7551bb8a8b795fad7e7226/constraintlayout-solver-2.0.1.jar:/Users/donghochoi/react-native-image-selector/android/build/intermediates/compile_only_not_namespaced_r_class_jar/debug/R.jar:/Users/donghochoi/Library/Android/sdk/platforms/android-28/android.jar:/Users/donghochoi/Library/Android/sdk/build-tools/28.0.3/core-lambda-stubs.jar" />
<option name="noStdlib" value="true" />
<option name="noReflect" value="true" />
<option name="moduleName" value="reactnativeimageselector_debug" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.util.Base64;

Expand Down Expand Up @@ -36,12 +38,17 @@ public static WritableMap createCacheFile(Context context, Uri fileUri, Readable

WritableMap response = Arguments.createMap();

response.putString("path", cacheFile.getPath());
response.putString("uri", "file://" + cacheFile.getPath());
response.putDouble("fileSize", (double) cacheFile.length());
response.putString("type", "image/jpeg");
response.putString("fileName", cacheFile.getName());
String base64EncodedString = FileManager.encodeBase64(cacheFile.getPath());
String photoPath = cacheFile.getPath();
double fileSize = (double) cacheFile.length();
String type = "image/jpeg";
String fileName = cacheFile.getName();

response.putString("path", photoPath);
response.putString("uri", "file://" + photoPath);
String base64EncodedString = FileManager.encodeBase64(photoPath);
response.putDouble("fileSize", fileSize);
response.putString("type", type);
response.putString("fileName", fileName);
response.putString("data", base64EncodedString);
return response;
}
Expand Down Expand Up @@ -96,10 +103,30 @@ public static File writeFileFromInputStream(Context context, InputStream inputSt
return null;
}

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

try {

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

ExifInterface exifInterface = new ExifInterface(targetFile.getPath());
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
break;
}

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
Expand Down Expand Up @@ -130,4 +157,10 @@ public static String encodeBase64(String path) {
byte[] byteArray = baos.toByteArray();
return Base64.encodeToString(byteArray, Base64.NO_WRAP);
}

public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
}

0 comments on commit 61f225b

Please sign in to comment.