ApfAdapter.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.epson.iprint.apf;
  2. import android.content.Context;
  3. import android.graphics.BitmapFactory;
  4. import android.media.ExifInterface;
  5. import android.os.Environment;
  6. import android.util.Log;
  7. import com.epson.p007sd.common.apf.apflib;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.Locale;
  11. import java.util.UUID;
  12. import java.util.concurrent.locks.ReentrantLock;
  13. import epson.common.ExternalFileUtils;
  14. import epson.common.ImageUtil;
  15. import epson.print.CommonDefine;
  16. import epson.print.EPImageUtil;
  17. import epson.print.MyPrinter;
  18. import epson.print.Util.BmpFileSize;
  19. public class ApfAdapter {
  20. protected static final String APF_IMAGE_DIR = "apf_image";
  21. public static final String BMP_EXTENSION = ".bmp";
  22. protected static final String FILE_PREFIX_APF = "apf";
  23. protected static final String FILE_PREFIX_TMP1 = "tmp1_";
  24. protected static final String FILE_PREFIX_TMP2 = "tmp2_";
  25. protected static final ReentrantLock mApfLock = new ReentrantLock();
  26. protected File mApfImageDir = new File(ExternalFileUtils.getInstance(this.mApplicationContext).getCacheDir(), APF_IMAGE_DIR);
  27. protected Context mApplicationContext;
  28. public ApfAdapter(Context context) {
  29. this.mApplicationContext = context.getApplicationContext();
  30. ExternalFileUtils.getInstance(this.mApplicationContext).createTempFolder(this.mApfImageDir.getPath());
  31. }
  32. public static void clearDirectory(Context context) {
  33. deleteDirectory(new File(ExternalFileUtils.getInstance(context).getCacheDir(), APF_IMAGE_DIR));
  34. }
  35. public static void deleteDirectory(File file) {
  36. if (file.exists()) {
  37. if (!file.isDirectory()) {
  38. file.delete();
  39. return;
  40. }
  41. for (File file2 : file.listFiles()) {
  42. if (file2.isDirectory()) {
  43. deleteDirectory(file2);
  44. }
  45. if (file2.exists()) {
  46. file2.delete();
  47. }
  48. }
  49. }
  50. }
  51. public static int getExifRotationValue(String str) {
  52. try {
  53. return new ExifInterface(str).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 0);
  54. } catch (IOException unused) {
  55. return 0;
  56. }
  57. }
  58. public static File rotateWithExifValue(int i, File file, File file2) {
  59. EPImageUtil ePImageUtil = new EPImageUtil();
  60. if (i == 3) {
  61. ePImageUtil.rotate180Image(file.toString(), file2.toString());
  62. return file2;
  63. } else if (i == 6) {
  64. ePImageUtil.rotateR90Image(file.toString(), file2.toString());
  65. return file2;
  66. } else if (i != 8) {
  67. return file;
  68. } else {
  69. ePImageUtil.rotateR270Image(file.toString(), file2.toString());
  70. return file2;
  71. }
  72. }
  73. public String getApfResult(String str, int i, int i2, int i3) {
  74. String apfFilename = getApfFilename(str, i, i2, i3);
  75. if (canUseApfResult(apfFilename)) {
  76. return apfFilename;
  77. }
  78. try {
  79. mApfLock.lockInterruptibly();
  80. if (!processApf(str, apfFilename, i == 1, i2 == 1, i3 == 1)) {
  81. mApfLock.unlock();
  82. return null;
  83. }
  84. mApfLock.unlock();
  85. return apfFilename;
  86. } catch (InterruptedException unused) {
  87. return null;
  88. } catch (Throwable th) {
  89. mApfLock.unlock();
  90. throw th;
  91. }
  92. }
  93. protected boolean canUseApfResult(String str) {
  94. return new File(str).exists();
  95. }
  96. public static String getMimeTypeWithBitmapFactory(String str) {
  97. BitmapFactory.Options options = new BitmapFactory.Options();
  98. options.inJustDecodeBounds = true;
  99. BitmapFactory.decodeFile(str, options);
  100. return options.outMimeType;
  101. }
  102. public boolean processApf(String str, String str2, boolean z, boolean z2, boolean z3) {
  103. String str3;
  104. int i;
  105. File file = null;
  106. try {
  107. String mimeTypeWithBitmapFactory = getMimeTypeWithBitmapFactory(str);
  108. if (CommonDefine.IMAGE_TYPE_BMP.equals(mimeTypeWithBitmapFactory)) {
  109. str3 = str;
  110. i = 0;
  111. } else if ("image/png".equals(mimeTypeWithBitmapFactory)) {
  112. file = convertPngToBmp(str);
  113. if (file != null) {
  114. str3 = file.toString();
  115. i = 0;
  116. } else {
  117. throw new OutOfMemoryError("in convertPngToBmp()");
  118. }
  119. } else if (!"image/jpeg".equals(mimeTypeWithBitmapFactory)) {
  120. return false;
  121. } else {
  122. file = convertJpegToBmp(str);
  123. if (file != null) {
  124. i = getExifRotationValue(str);
  125. str3 = file.toString();
  126. } else {
  127. throw new OutOfMemoryError("in convertJpegToBmp()");
  128. }
  129. }
  130. apflib apflib = new apflib();
  131. apflib.interrupt();
  132. boolean apfFile = apflib.apfFile(str, i, str3, str2, z, z2, z3);
  133. Log.v("ApfAdapter", "apf returns <" + apfFile + ">");
  134. if (!apfFile) {
  135. throw new OutOfMemoryError("in apflib.apfFile()");
  136. } else if (!BmpFileSize.checkDiskCapacity(Environment.getExternalStorageDirectory().getPath(), 8192)) {
  137. throw new OutOfMemoryError("in apflib.apfFile() size check");
  138. } else if (file == null || !file.exists()) {
  139. return true;
  140. } else {
  141. file.delete();
  142. return true;
  143. }
  144. } catch (OutOfMemoryError e) {
  145. throw e;
  146. } catch (Exception unused) {
  147. if (file != null && file.exists()) {
  148. file.delete();
  149. }
  150. return false;
  151. } catch (Throwable th) {
  152. if (file != null && file.exists()) {
  153. file.delete();
  154. }
  155. throw th;
  156. }
  157. }
  158. protected File convertPngToBmp(String str) {
  159. String png2jpeg = ImageUtil.png2jpeg(this.mApplicationContext, str, 1);
  160. if (png2jpeg == null) {
  161. return null;
  162. }
  163. File file = new File(new File(ExternalFileUtils.getInstance(this.mApplicationContext).getTempViewDir()), png2jpeg);
  164. if (!file.exists()) {
  165. return null;
  166. }
  167. try {
  168. File file2 = new File(getTmpBmpFilename(str, FILE_PREFIX_TMP1));
  169. if (new EPImageUtil().jpg2bmp(file.toString(), file2.toString(), 1) != 0) {
  170. return null;
  171. }
  172. file.delete();
  173. return file2;
  174. } finally {
  175. file.delete();
  176. }
  177. }
  178. protected File convertJpegToBmp(String str) {
  179. File file;
  180. File file2 = null;
  181. try {
  182. file = new File(getTmpBmpFilename(str, FILE_PREFIX_TMP1));
  183. try {
  184. if (new EPImageUtil().jpg2bmp(str, file.toString(), 1) != 0) {
  185. if (file.exists()) {
  186. file.delete();
  187. }
  188. return null;
  189. }
  190. int exifRotationValue = getExifRotationValue(str);
  191. File file3 = new File(getTmpBmpFilename(str, FILE_PREFIX_TMP2));
  192. try {
  193. File rotateWithExifValue = rotateWithExifValue(exifRotationValue, file, file3);
  194. if (rotateWithExifValue != file && file.exists()) {
  195. file.delete();
  196. }
  197. if (rotateWithExifValue != file3 && file3.exists()) {
  198. file3.delete();
  199. }
  200. return rotateWithExifValue;
  201. } catch (Throwable th) {
  202. th = th;
  203. file2 = file3;
  204. if (!(file == null || file == null || !file.exists())) {
  205. file.delete();
  206. }
  207. if (!(file2 == null || file2 == null || !file2.exists())) {
  208. file2.delete();
  209. }
  210. throw th;
  211. }
  212. } catch (Throwable th2) {
  213. th = th2;
  214. file.delete();
  215. file2.delete();
  216. throw th;
  217. }
  218. } catch (Throwable th3) {
  219. th = th3;
  220. file = null;
  221. file.delete();
  222. file2.delete();
  223. throw th;
  224. }
  225. }
  226. protected String getTmpBmpFilename(String str, String str2) {
  227. if (this.mApfImageDir == null) {
  228. return null;
  229. }
  230. UUID nameUUIDFromBytes = UUID.nameUUIDFromBytes(str.getBytes());
  231. File file = this.mApfImageDir;
  232. return new File(file, str2 + nameUUIDFromBytes.toString() + ".bmp").toString();
  233. }
  234. public String getApfFilename(String str, int i, int i2, int i3) {
  235. return getTmpBmpFilename(str, String.format(Locale.US, "%s%ds%dc%d_", new Object[]{FILE_PREFIX_APF, Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3)}));
  236. }
  237. public static String getCurrentPrinterId(Context context) {
  238. String name = MyPrinter.getCurPrinter(context).getName();
  239. if (name == null) {
  240. return "";
  241. }
  242. return name.replaceFirst("(?i)^epson +", "");
  243. }
  244. public static boolean isEpsonColor(Context context, int i, int i2, boolean z) {
  245. return new apflib().isDspEpsonColorLogo(getCurrentPrinterId(context), i, i2, z);
  246. }
  247. }