Skip to content

Commit

Permalink
Proper fix using ExifInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Jul 6, 2016
1 parent ceac6d6 commit 96fe719
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 48 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="fr.remram.taquindroid"
android:versionCode="1002000"
android:versionName="1.2">
<uses-sdk android:minSdkVersion="3"/>
<uses-sdk android:minSdkVersion="5"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Menu"
Expand Down
47 changes: 1 addition & 46 deletions app/src/main/java/fr/remram/taquindroid/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Log;

import java.io.FileDescriptor;
import java.io.IOException;

public class Game extends Activity implements GameView.EndGameListener {

Expand All @@ -38,27 +30,7 @@ public void onCreate(Bundle savedInstanceState)
m_SelectedImage = intent.getData();
Bitmap image = null;
if(m_SelectedImage != null)
{
try {
ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(m_SelectedImage, "r");
if(descriptor != null)
{
FileDescriptor fileDescriptor = descriptor.getFileDescriptor();
image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
descriptor.close();

int orientation = getOrientation(m_SelectedImage);
if(orientation > 0)
{
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
}
}
} catch(IOException e) {
Log.e("TAQUINDROID", "Couldn't open requested image file", e);
}
}
image = ImageTools.getBitmap(this, m_SelectedImage);

if(image == null)
image = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Expand Down Expand Up @@ -95,21 +67,4 @@ public void onGameEnded(int moves)
finish();
}

// From http://stackoverflow.com/a/8661569/711380
public int getOrientation(Uri photoUri)
{
Cursor cursor = getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

int result = -1;
if (cursor != null) {
if (cursor.moveToFirst()) {
result = cursor.getInt(0);
}
cursor.close();
}

return result;
}

}
140 changes: 140 additions & 0 deletions app/src/main/java/fr/remram/taquindroid/ImageTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package fr.remram.taquindroid;

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.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageTools {

public static String getExtension(String path)
{
int index = path.lastIndexOf(".");
if(index > 0 && index < path.length() - 1 && index >= path.length() - 5)
return path.substring(index).toLowerCase();
else
return "";
}

public static int getOrientation(Context context, Uri photoUri)
{
try {
InputStream istream = context.getContentResolver().openInputStream(photoUri);
if(istream == null)
return ExifInterface.ORIENTATION_UNDEFINED;

File outputDir = context.getCacheDir();
String extension = getExtension(photoUri.getPath());
File tempfile = File.createTempFile("taquindroid", extension, outputDir);
FileOutputStream ostream = new FileOutputStream(tempfile);

byte[] buffer = new byte[4096];
int len;
while((len = istream.read(buffer)) != -1)
ostream.write(buffer, 0, len);
ostream.close();
istream.close();

ExifInterface exif = new ExifInterface(tempfile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
if(!tempfile.delete())
Log.e("TAQUINDROID", String.format("Couldn't delete teporary file %s", tempfile.getAbsolutePath()));
return orientation;
} catch(IOException e) {
Log.e("TAQUINDROID", "Error reading image orientation");
return ExifInterface.ORIENTATION_UNDEFINED;
}
}

public static Matrix getImageMatrix(int orientation)
{
Matrix matrix = new Matrix();
switch(orientation)
{
case ExifInterface.ORIENTATION_NORMAL:
return null;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return null;
}
return matrix;
}

public static Matrix getImageMatrix(Context context, Uri photoUri)
{
return getImageMatrix(getOrientation(context, photoUri));
}

public static Bitmap orientBitmap(Bitmap image, int orientation)
{
Matrix matrix = getImageMatrix(orientation);
if(matrix == null)
return image;
try {
Bitmap image2 = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
image.recycle();
return image2;
}
catch(OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}

public static Bitmap getBitmap(Context context, Uri uri)
{
try {
ParcelFileDescriptor descriptor = context.getContentResolver().openFileDescriptor(uri, "r");
if(descriptor != null)
{
FileDescriptor fileDescriptor = descriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
descriptor.close();

int orientation = ImageTools.getOrientation(context, uri);
image = ImageTools.orientBitmap(image, orientation);

return image;
}
else
return null;
} catch(IOException e) {
return null;
}
}

}
9 changes: 8 additions & 1 deletion app/src/main/java/fr/remram/taquindroid/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
Expand Down Expand Up @@ -128,7 +129,13 @@ protected void updateImage()
{
ImageView image = (ImageView) findViewById(R.id.preview_image);
if(m_SelectedImage != null)
image.setImageURI(m_SelectedImage);
{
Matrix matrix = ImageTools.getImageMatrix(this, m_SelectedImage);
if(matrix == null)
image.setImageURI(m_SelectedImage);
else
image.setImageBitmap(ImageTools.getBitmap(this, m_SelectedImage));
}
else
image.setImageDrawable(getResources().getDrawable(R.drawable.image));
}
Expand Down

0 comments on commit 96fe719

Please sign in to comment.