ImageResizer.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package epson.print.imgsel;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Matrix;
  4. import android.media.ExifInterface;
  5. import android.os.AsyncTask;
  6. import android.util.Log;
  7. import epson.common.ImageUtil;
  8. import epson.print.EPImage;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.lang.ref.WeakReference;
  12. public class ImageResizer {
  13. private static ImageResizer sImageResizer;
  14. /* access modifiers changed from: private */
  15. public BitmapCache mBitmapCache = new BitmapCache();
  16. /* access modifiers changed from: private */
  17. public volatile boolean mIsTargetActivityForeground;
  18. private int mReqSize;
  19. public interface BitmapDrawer {
  20. void setBitmap(Bitmap bitmap);
  21. }
  22. public static synchronized ImageResizer getInstance(int i, File file) {
  23. ImageResizer imageResizer;
  24. synchronized (ImageResizer.class) {
  25. if (sImageResizer == null) {
  26. sImageResizer = new ImageResizer(i, file);
  27. }
  28. imageResizer = sImageResizer;
  29. }
  30. return imageResizer;
  31. }
  32. private ImageResizer(int i, File file) {
  33. this.mReqSize = i;
  34. this.mBitmapCache.initialize(file);
  35. }
  36. public void setActivityForeground(boolean z) {
  37. this.mIsTargetActivityForeground = z;
  38. if (!z) {
  39. this.mBitmapCache.saveJournal();
  40. }
  41. }
  42. public ResizeTask load(String str, BitmapDrawer bitmapDrawer) {
  43. File file = new File(str);
  44. Bitmap bitmap = this.mBitmapCache.getBitmap(file);
  45. if (bitmap != null) {
  46. updateImageViewBitmap(bitmap, bitmapDrawer);
  47. return null;
  48. }
  49. ResizeTask resizeTask = new ResizeTask(file, bitmapDrawer, this.mReqSize);
  50. resizeTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
  51. return resizeTask;
  52. }
  53. private void updateImageViewBitmap(Bitmap bitmap, BitmapDrawer bitmapDrawer) {
  54. if (bitmap != null && bitmapDrawer != null) {
  55. bitmapDrawer.setBitmap(bitmap);
  56. }
  57. }
  58. public class ResizeTask extends AsyncTask<Void, Void, Bitmap> {
  59. private WeakReference<BitmapDrawer> mImageImageDrawerReference;
  60. private File mOrgFile;
  61. private int mReqSize;
  62. public ResizeTask(File file, BitmapDrawer bitmapDrawer, int i) {
  63. this.mOrgFile = file;
  64. this.mImageImageDrawerReference = new WeakReference<>(bitmapDrawer);
  65. this.mReqSize = i;
  66. }
  67. /* access modifiers changed from: protected */
  68. public Bitmap doInBackground(Void... voidArr) {
  69. File file;
  70. Bitmap bitmap;
  71. if (isCancelled() || !ImageResizer.this.mIsTargetActivityForeground || (file = this.mOrgFile) == null) {
  72. return null;
  73. }
  74. try {
  75. bitmap = ImageUtil.loadBitmap(file.toString(), this.mReqSize, this.mReqSize, true, true);
  76. } catch (OutOfMemoryError e) {
  77. Log.w("ImageResizer", "doInBackground:" + e.toString());
  78. bitmap = null;
  79. }
  80. if (isCancelled()) {
  81. return null;
  82. }
  83. if (bitmap == null) {
  84. return bitmap;
  85. }
  86. Bitmap rotateBitmap = rotateBitmap(this.mOrgFile, bitmap);
  87. ImageResizer.this.mBitmapCache.addBitmap(this.mOrgFile, rotateBitmap);
  88. return rotateBitmap;
  89. }
  90. private void testSleep(int i) {
  91. try {
  92. Thread.sleep((long) i);
  93. } catch (InterruptedException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. private Bitmap rotateBitmap(File file, Bitmap bitmap) {
  98. int exifRotation = getExifRotation(file);
  99. if (exifRotation == 0) {
  100. return bitmap;
  101. }
  102. Matrix matrix = new Matrix();
  103. matrix.setRotate((float) exifRotation);
  104. return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  105. }
  106. private int getExifRotation(File file) {
  107. try {
  108. int attributeInt = new ExifInterface(file.toString()).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 1);
  109. if (attributeInt == 3) {
  110. return EPImage.EPS_ROTATE_180;
  111. }
  112. if (attributeInt == 6) {
  113. return 90;
  114. }
  115. if (attributeInt != 8) {
  116. return 0;
  117. }
  118. return EPImage.EPS_ROTATE_270;
  119. } catch (IOException unused) {
  120. return 0;
  121. }
  122. }
  123. /* access modifiers changed from: protected */
  124. public void onPostExecute(Bitmap bitmap) {
  125. BitmapDrawer bitmapDrawer = (BitmapDrawer) this.mImageImageDrawerReference.get();
  126. if (bitmap != null && bitmapDrawer != null) {
  127. bitmapDrawer.setBitmap(bitmap);
  128. }
  129. }
  130. }
  131. }