pdfRender.java 12 KB

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