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