properly rotate avatars

This commit is contained in:
Daniel Gultsch 2016-01-04 15:17:02 +01:00
parent 95bf0630f0
commit f815a7cd26
4 changed files with 48 additions and 52 deletions

View file

@ -42,9 +42,6 @@ import eu.siacs.conversations.utils.FileUtils;
import eu.siacs.conversations.xmpp.pep.Avatar; import eu.siacs.conversations.xmpp.pep.Avatar;
public class FileBackend { public class FileBackend {
private static int IMAGE_SIZE = 1920;
private final SimpleDateFormat imageDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US); private final SimpleDateFormat imageDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);
private XmppConnectionService mXmppConnectionService; private XmppConnectionService mXmppConnectionService;
@ -116,7 +113,10 @@ public class FileBackend {
} }
} }
public Bitmap rotate(Bitmap bitmap, int degree) { public static Bitmap rotate(Bitmap bitmap, int degree) {
if (degree == 0) {
return bitmap;
}
int w = bitmap.getWidth(); int w = bitmap.getWidth();
int h = bitmap.getHeight(); int h = bitmap.getHeight();
Matrix mtx = new Matrix(); Matrix mtx = new Matrix();
@ -226,9 +226,7 @@ public class FileBackend {
} }
Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE); Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
int rotation = getRotation(image); int rotation = getRotation(image);
if (rotation > 0) {
scaledBitmap = rotate(scaledBitmap, rotation); scaledBitmap = rotate(scaledBitmap, rotation);
}
boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, Config.IMAGE_QUALITY, os); boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, Config.IMAGE_QUALITY, os);
if (!success) { if (!success) {
throw new FileCopyException(R.string.error_compressing_image); throw new FileCopyException(R.string.error_compressing_image);
@ -289,10 +287,7 @@ public class FileBackend {
throw new FileNotFoundException(); throw new FileNotFoundException();
} }
thumbnail = resize(fullsize, size); thumbnail = resize(fullsize, size);
int rotation = getRotation(file); thumbnail = rotate(thumbnail, getRotation(file));
if (rotation > 0) {
thumbnail = rotate(thumbnail, rotation);
}
this.mXmppConnectionService.getBitmapCache().put(message.getUuid(),thumbnail); this.mXmppConnectionService.getBitmapCache().put(message.getUuid(),thumbnail);
} }
return thumbnail; return thumbnail;
@ -404,10 +399,7 @@ public class FileBackend {
if (input == null) { if (input == null) {
return null; return null;
} else { } else {
int rotation = getRotation(image); input = rotate(input, getRotation(image));
if (rotation > 0) {
input = rotate(input, rotation);
}
return cropCenterSquare(input, size); return cropCenterSquare(input, size);
} }
} catch (SecurityException e) { } catch (SecurityException e) {

View file

@ -2215,8 +2215,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
final UiCallback<Avatar> callback) { final UiCallback<Avatar> callback) {
final Bitmap.CompressFormat format = Config.AVATAR_FORMAT; final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
final int size = Config.AVATAR_SIZE; final int size = Config.AVATAR_SIZE;
final Avatar avatar = getFileBackend() final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
.getPepAvatar(image, size, format);
if (avatar != null) { if (avatar != null) {
avatar.height = size; avatar.height = size;
avatar.width = size; avatar.width = size;

View file

@ -23,6 +23,8 @@ import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.utils.ExifHelper;
import eu.siacs.conversations.utils.FileUtils;
import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
@ -31,17 +33,16 @@ import eu.siacs.conversations.xmpp.pep.Avatar;
public class PublishProfilePictureActivity extends XmppActivity { public class PublishProfilePictureActivity extends XmppActivity {
private static final int REQUEST_CHOOSE_FILE = 0xac23; private static final int REQUEST_CHOOSE_FILE = 0xac23;
private ImageView avatar; private ImageView avatar;
private TextView accountTextView; private TextView accountTextView;
private TextView hintOrWarning; private TextView hintOrWarning;
private TextView secondaryHint; private TextView secondaryHint;
private Button cancelButton; private Button cancelButton;
private Button publishButton; private Button publishButton;
final static int REQUEST_CROP_PICTURE = 92374;
private Uri avatarUri; private Uri avatarUri;
private Uri defaultUri; private Uri defaultUri;
private Account account;
private boolean support = false;
private OnLongClickListener backToDefaultListener = new OnLongClickListener() { private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
@Override @Override
@ -51,8 +52,6 @@ public class PublishProfilePictureActivity extends XmppActivity {
return true; return true;
} }
}; };
private Account account;
private boolean support = false;
private boolean mInitialAccountSetup; private boolean mInitialAccountSetup;
private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() { private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() {
@ -140,8 +139,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
Intent attachFileIntent = new Intent(); Intent attachFileIntent = new Intent();
attachFileIntent.setType("image/*"); attachFileIntent.setType("image/*");
attachFileIntent.setAction(Intent.ACTION_GET_CONTENT); attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent chooser = Intent.createChooser(attachFileIntent, Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file));
getString(R.string.attach_file));
startActivityForResult(chooser, REQUEST_CHOOSE_FILE); startActivityForResult(chooser, REQUEST_CHOOSE_FILE);
} }
}); });
@ -149,19 +147,25 @@ public class PublishProfilePictureActivity extends XmppActivity {
} }
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
final Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CHOOSE_FILE) { switch (requestCode) {
this.avatarUri = data.getData(); case REQUEST_CHOOSE_FILE:
Uri source = data.getData();
String original = FileUtils.getPath(this, source);
if (original != null) {
source = Uri.parse("file://"+original);
}
Uri destination = Uri.fromFile(new File(getCacheDir(), "croppedAvatar")); Uri destination = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
Crop.of(this.avatarUri, destination).asSquare().start(PublishProfilePictureActivity.this); final int size = getPixel(192);
} Crop.of(source, destination).asSquare().withMaxSize(size, size).start(this);
} break;
if (requestCode == Crop.REQUEST_CROP) { case Crop.REQUEST_CROP:
this.avatarUri = Uri.fromFile(new File(getCacheDir(), "croppedAvatar")); this.avatarUri = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
loadImageIntoPreview(this.avatarUri); loadImageIntoPreview(this.avatarUri);
break;
}
} }
} }
@ -182,8 +186,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
if (this.avatarUri == null) { if (this.avatarUri == null) {
if (this.account.getAvatar() != null if (this.account.getAvatar() != null
|| this.defaultUri == null) { || this.defaultUri == null) {
this.avatar.setImageBitmap(avatarService().get(account, this.avatar.setImageBitmap(avatarService().get(account, getPixel(192)));
getPixel(194)));
if (this.defaultUri != null) { if (this.defaultUri != null) {
this.avatar this.avatar
.setOnLongClickListener(this.backToDefaultListener); .setOnLongClickListener(this.backToDefaultListener);
@ -220,8 +223,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
if (getIntent() != null) { if (getIntent() != null) {
this.mInitialAccountSetup = getIntent().getBooleanExtra("setup", this.mInitialAccountSetup = getIntent().getBooleanExtra("setup", false);
false);
} }
if (this.mInitialAccountSetup) { if (this.mInitialAccountSetup) {
this.cancelButton.setText(R.string.skip); this.cancelButton.setText(R.string.skip);
@ -231,15 +233,18 @@ public class PublishProfilePictureActivity extends XmppActivity {
private Bitmap loadScaledBitmap(Uri uri, int reqSize) throws FileNotFoundException { private Bitmap loadScaledBitmap(Uri uri, int reqSize) throws FileNotFoundException {
final BitmapFactory.Options options = new BitmapFactory.Options(); final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
int rotation = ExifHelper.getOrientation(getContentResolver().openInputStream(uri));
options.inSampleSize = FileBackend.calcSampleSize(options, reqSize); options.inSampleSize = FileBackend.calcSampleSize(options, reqSize);
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
return FileBackend.rotate(bm,rotation);
} }
protected void loadImageIntoPreview(Uri uri) { protected void loadImageIntoPreview(Uri uri) {
Bitmap bm = null; Bitmap bm = null;
try { try {
bm = loadScaledBitmap(uri, Config.AVATAR_SIZE); bm = loadScaledBitmap(uri, getPixel(192));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -16,8 +16,8 @@
<ImageView <ImageView
android:id="@+id/account_image" android:id="@+id/account_image"
android:layout_width="194dp" android:layout_width="192dp"
android:layout_height="194dp" /> android:layout_height="192dp" />
</LinearLayout> </LinearLayout>
<TextView <TextView