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