pdfRender.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package epson.print.pdf;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.support.annotation.NonNull;
  8. import android.util.Log;
  9. import epson.common.ExternalFileUtils;
  10. import epson.print.CommonDefine;
  11. import epson.print.EPImage;
  12. import epson.print.Util.EPLog;
  13. import epson.print.pdf.AdobePdfContainer;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. public class pdfRender implements CommonDefine {
  20. private static final float DEF_ZOOM_PREVIEW = 0.5f;
  21. private static final float DEF_ZOOM_PRINT = 1.5f;
  22. private static final String TAG = "PDF_CONVERTER";
  23. /* access modifiers changed from: private */
  24. public Thread backgroundThread;
  25. private Context mContext = null;
  26. private String mConvertedName;
  27. private String mConvertedNamePrint;
  28. protected volatile boolean mIsPreviewCon;
  29. protected boolean mOpenError = false;
  30. /* access modifiers changed from: private */
  31. public AdobePdfContainer mPdfFile;
  32. private String mPdfFilename;
  33. protected boolean mPreviewmode = false;
  34. protected volatile boolean mPrintingCon;
  35. protected volatile boolean mRendererStart = false;
  36. protected boolean mRotate = false;
  37. protected boolean mRotateCheck = false;
  38. public int mTotalPage;
  39. /* access modifiers changed from: private */
  40. public Handler mUiHandler;
  41. public int StartReloadDocument(String str) {
  42. return 0;
  43. }
  44. public void finalize() {
  45. }
  46. public pdfRender(Context context, Handler handler) {
  47. this.mContext = context;
  48. this.mUiHandler = handler;
  49. }
  50. public synchronized void startConvertPage(final int i, final int i2, final int i3) {
  51. if (this.backgroundThread == null) {
  52. this.mRendererStart = false;
  53. this.backgroundThread = new Thread(new Runnable() {
  54. public void run() {
  55. try {
  56. if (pdfRender.this.mPdfFile != null) {
  57. if (i3 == 0) {
  58. pdfRender.this.mPreviewmode = true;
  59. pdfRender.this.mUiHandler.sendEmptyMessage(4);
  60. Log.d(pdfRender.TAG, "convert4Preview :start");
  61. pdfRender.this.convert4Preview(i);
  62. Log.d(pdfRender.TAG, "convert4Preview :finish");
  63. } else if (i3 == 1) {
  64. pdfRender.this.mPreviewmode = false;
  65. Log.d(pdfRender.TAG, "printing");
  66. pdfRender.this.convert4Print(i, i2);
  67. }
  68. }
  69. } catch (Exception e) {
  70. Log.e(pdfRender.TAG, "GOT ERROR WHEN CONVERT: " + e);
  71. }
  72. Thread unused = pdfRender.this.backgroundThread = null;
  73. }
  74. });
  75. this.backgroundThread.start();
  76. }
  77. }
  78. public boolean RendererStart() {
  79. return this.mRendererStart;
  80. }
  81. public boolean openPdfFile(String str, String str2, Context context) {
  82. boolean z;
  83. this.mRotateCheck = false;
  84. this.mPdfFile = new AdobePdfContainer(context);
  85. this.mPdfFilename = str;
  86. try {
  87. boolean parsePDF = parsePDF(str, str2);
  88. z = true;
  89. if (!parsePDF) {
  90. z = false;
  91. }
  92. try {
  93. if (this.mPdfFile == null || z) {
  94. return z;
  95. }
  96. this.mTotalPage = this.mPdfFile.CountPages();
  97. this.mUiHandler.sendEmptyMessage(2);
  98. return false;
  99. } catch (Exception e) {
  100. e = e;
  101. Log.e(TAG, "GOT ERROR WHEN CONVERT: " + e);
  102. return z;
  103. }
  104. } catch (Exception e2) {
  105. e = e2;
  106. z = false;
  107. Log.e(TAG, "GOT ERROR WHEN CONVERT: " + e);
  108. return z;
  109. }
  110. }
  111. public boolean canPrintableDoc() {
  112. return this.mPdfFile.hasPrintPermission() == 0;
  113. }
  114. public void convert4Preview(int i) {
  115. ExternalFileUtils.getInstance(this.mContext).initPdfDir();
  116. this.mIsPreviewCon = true;
  117. if (convertPage(i, DEF_ZOOM_PREVIEW, 0, 1)) {
  118. Log.d(TAG, "convertPage Preview Finish :" + i);
  119. try {
  120. Thread.sleep(200);
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. if (this.mIsPreviewCon) {
  125. Bundle bundle = new Bundle();
  126. bundle.putInt(CommonDefine.CONVERTED_PAGE, i);
  127. Message message = new Message();
  128. message.setData(bundle);
  129. message.what = 0;
  130. this.mUiHandler.sendMessage(message);
  131. return;
  132. }
  133. Log.e(TAG, "Convert Preview NG : mIsPreviewCon = false");
  134. ExternalFileUtils.getInstance(this.mContext).initPdfDir();
  135. }
  136. }
  137. public void convert4Print(int i, int i2) {
  138. ExternalFileUtils.getInstance(this.mContext).initPrintDir();
  139. this.mPrintingCon = true;
  140. int i3 = 1;
  141. while (this.mPrintingCon) {
  142. if (i - 1 <= i2 - 1) {
  143. try {
  144. if (this.mPrintingCon) {
  145. ExternalFileUtils.getInstance(this.mContext).initPdfDir();
  146. if (convertPage(i, DEF_ZOOM_PRINT, 1, i3)) {
  147. this.mRendererStart = true;
  148. i++;
  149. i3++;
  150. } else {
  151. return;
  152. }
  153. }
  154. } catch (Exception e) {
  155. e.printStackTrace();
  156. }
  157. }
  158. this.mPrintingCon = false;
  159. return;
  160. }
  161. }
  162. public boolean rotate() {
  163. while (!this.mRotateCheck) {
  164. try {
  165. Thread.sleep(10);
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. }
  169. if (this.mOpenError) {
  170. break;
  171. }
  172. }
  173. return this.mRotate;
  174. }
  175. private boolean convertPage(int i, float f, int i2, int i3) {
  176. int i4;
  177. AdobePdfContainer adobePdfContainer = this.mPdfFile;
  178. if (adobePdfContainer != null) {
  179. try {
  180. float GetPageSizeX = (float) adobePdfContainer.GetPageSizeX(i);
  181. float GetPageSizeY = (float) this.mPdfFile.GetPageSizeY(i);
  182. EPLog.i(TAG, new File(this.mPdfFilename).getName() + " - " + i + CommonDefine.SLASH + this.mTotalPage + ": " + GetPageSizeX + "x" + GetPageSizeY);
  183. if (i == 1) {
  184. if (GetPageSizeX > GetPageSizeY) {
  185. this.mRotate = true;
  186. } else {
  187. this.mRotate = false;
  188. }
  189. this.mRotateCheck = true;
  190. i4 = 0;
  191. } else if (GetPageSizeX > GetPageSizeY) {
  192. i4 = !this.mRotate ? 90 : 0;
  193. } else {
  194. i4 = !this.mRotate ? 0 : EPImage.EPS_ROTATE_270;
  195. }
  196. int i5 = (GetPageSizeX > GetPageSizeY ? 1 : (GetPageSizeX == GetPageSizeY ? 0 : -1));
  197. float f2 = (float) (!this.mPreviewmode ? 4209 : 702);
  198. float f3 = f2 / GetPageSizeX;
  199. float f4 = f2 / GetPageSizeY;
  200. if (f3 <= f4) {
  201. f4 = f3;
  202. }
  203. float f5 = GetPageSizeX * f4;
  204. float f6 = f4 * GetPageSizeY;
  205. EPLog.i(TAG, "scaledWidth = " + f5 + ",scaledHeight = " + f6);
  206. System.gc();
  207. this.mConvertedName = "";
  208. if (i2 != 1) {
  209. Bitmap pageBitmap = this.mPdfFile.getPageBitmap(i, (int) f5, (int) f6, i4);
  210. if (pageBitmap == null) {
  211. this.mUiHandler.sendEmptyMessage(6);
  212. return false;
  213. }
  214. String pdfDir = ExternalFileUtils.getInstance(this.mContext).getPdfDir();
  215. this.mConvertedName = new File(pdfDir, CommonDefine.UNDER_BAR + i + ".jpg").getPath();
  216. saveImageToJpg(pageBitmap, this.mConvertedName);
  217. pageBitmap.recycle();
  218. Log.d(TAG, "saveImageToJpg Finish : " + this.mConvertedName);
  219. } else {
  220. String pageBitmapFile = this.mPdfFile.getPageBitmapFile(i, (int) f5, (int) f6, i4 != 0);
  221. if (pageBitmapFile == null) {
  222. this.mUiHandler.sendEmptyMessage(6);
  223. return false;
  224. }
  225. String decodeJpegFilename = getDecodeJpegFilename(i);
  226. new File(pageBitmapFile).renameTo(new File(decodeJpegFilename));
  227. Log.d(TAG, "renameTo Finish : " + decodeJpegFilename);
  228. }
  229. } catch (Exception e) {
  230. Log.e(TAG, "GOT ERROR WHEN CONVERT: " + e.toString());
  231. }
  232. }
  233. return true;
  234. }
  235. @NonNull
  236. public String getDecodeJpegFilename(int i) {
  237. String printDir = ExternalFileUtils.getInstance(this.mContext).getPrintDir();
  238. return new File(printDir, CommonDefine.UNDER_BAR + i + ".jpg").getPath();
  239. }
  240. private void saveImageToJpg(Bitmap bitmap, String str) {
  241. try {
  242. FileOutputStream fileOutputStream = new FileOutputStream(new File(str));
  243. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  244. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
  245. fileOutputStream.write(byteArrayOutputStream.toByteArray());
  246. fileOutputStream.flush();
  247. fileOutputStream.close();
  248. Log.i(TAG, str + " converted");
  249. } catch (IOException e) {
  250. e.printStackTrace();
  251. }
  252. }
  253. private boolean parsePDF(String str, String str2) {
  254. try {
  255. long length = new File(str).length();
  256. if (length == 0) {
  257. Log.e(TAG, "file is empty");
  258. this.mUiHandler.sendEmptyMessage(6);
  259. return false;
  260. }
  261. Log.i(TAG, "file: " + this.mPdfFilename + "has" + length + "byte");
  262. openFile(str, str2);
  263. return false;
  264. } catch (AdobePdfContainer.PasswordException unused) {
  265. Log.i(TAG, "Password Locked");
  266. return true;
  267. } catch (Throwable th) {
  268. th.printStackTrace();
  269. this.mUiHandler.sendEmptyMessage(6);
  270. this.mOpenError = true;
  271. return false;
  272. }
  273. }
  274. public void openFile(String str, String str2) throws AdobePdfContainer.PasswordException, AdobePdfContainer.NoPdfException {
  275. this.mPdfFile.LoadDocument(str, str2);
  276. }
  277. private byte[] readBytes(File file) throws IOException {
  278. int length = (int) file.length();
  279. byte[] bArr = new byte[length];
  280. FileInputStream fileInputStream = new FileInputStream(file);
  281. int i = 0;
  282. int read = fileInputStream.read(bArr, 0, length + 0);
  283. while (read > 0) {
  284. i += read;
  285. read = fileInputStream.read(bArr, i, length - i);
  286. }
  287. return bArr;
  288. }
  289. public boolean isFileExit() {
  290. return this.mPdfFile != null;
  291. }
  292. public int totalPages() {
  293. AdobePdfContainer adobePdfContainer = this.mPdfFile;
  294. if (adobePdfContainer != null) {
  295. return adobePdfContainer.CountPages();
  296. }
  297. return 0;
  298. }
  299. public String getConvertedPagePrint() {
  300. return this.mConvertedNamePrint;
  301. }
  302. public String getConvertedPagePreview() {
  303. return this.mConvertedName;
  304. }
  305. public void setPrintingStt(boolean z) {
  306. try {
  307. Thread.sleep(200);
  308. } catch (Exception e) {
  309. e.printStackTrace();
  310. }
  311. this.mPrintingCon = z;
  312. if (!z) {
  313. while (this.backgroundThread != null && this.backgroundThread.isAlive()) {
  314. try {
  315. Thread.sleep(2000);
  316. EPLog.d("Convert Preview", "convert4Print shutdown...");
  317. } catch (InterruptedException | NullPointerException unused) {
  318. return;
  319. }
  320. }
  321. }
  322. }
  323. public void setMIsPreviewCon(boolean z) {
  324. this.mIsPreviewCon = z;
  325. }
  326. }