ImageResizer.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. protected Bitmap doInBackground(Void... voidArr) {
  68. File file;
  69. Bitmap bitmap;
  70. if (isCancelled() || !ImageResizer.this.mIsTargetActivityForeground || (file = this.mOrgFile) == null) {
  71. return null;
  72. }
  73. try {
  74. bitmap = ImageUtil.loadBitmap(file.toString(), this.mReqSize, this.mReqSize, true, true);
  75. } catch (OutOfMemoryError e) {
  76. Log.w("ImageResizer", "doInBackground:" + e.toString());
  77. bitmap = null;
  78. }
  79. if (isCancelled()) {
  80. return null;
  81. }
  82. if (bitmap == null) {
  83. return bitmap;
  84. }
  85. Bitmap rotateBitmap = rotateBitmap(this.mOrgFile, bitmap);
  86. ImageResizer.this.mBitmapCache.addBitmap(this.mOrgFile, rotateBitmap);
  87. return rotateBitmap;
  88. }
  89. private void testSleep(int i) {
  90. try {
  91. Thread.sleep((long) i);
  92. } catch (InterruptedException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. private Bitmap rotateBitmap(File file, Bitmap bitmap) {
  97. int exifRotation = getExifRotation(file);
  98. if (exifRotation == 0) {
  99. return bitmap;
  100. }
  101. Matrix matrix = new Matrix();
  102. matrix.setRotate((float) exifRotation);
  103. return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  104. }
  105. private int getExifRotation(File file) {
  106. try {
  107. int attributeInt = new ExifInterface(file.toString()).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 1);
  108. if (attributeInt == 3) {
  109. return EPImage.EPS_ROTATE_180;
  110. }
  111. if (attributeInt == 6) {
  112. return 90;
  113. }
  114. if (attributeInt != 8) {
  115. return 0;
  116. }
  117. return EPImage.EPS_ROTATE_270;
  118. } catch (IOException unused) {
  119. return 0;
  120. }
  121. }
  122. protected void onPostExecute(Bitmap bitmap) {
  123. BitmapDrawer bitmapDrawer = (BitmapDrawer) this.mImageImageDrawerReference.get();
  124. if (bitmap != null && bitmapDrawer != null) {
  125. bitmapDrawer.setBitmap(bitmap);
  126. }
  127. }
  128. }
  129. }