PdfPage.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package org.vudroid.pdfdroid.codec;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Matrix;
  4. import android.graphics.Rect;
  5. import android.graphics.RectF;
  6. import com.poqop.document.codec.CodecPage;
  7. import java.nio.ByteBuffer;
  8. public class PdfPage implements CodecPage
  9. {
  10. private long pageHandle;
  11. private long docHandle;
  12. private PdfPage(long pageHandle, long docHandle)
  13. {
  14. this.pageHandle = pageHandle;
  15. this.docHandle = docHandle;
  16. }
  17. public boolean isDecoding()
  18. {
  19. return false; //TODO
  20. }
  21. public void waitForDecode()
  22. {
  23. //TODO
  24. }
  25. public int getWidth()
  26. {
  27. return (int) getMediaBox().width();
  28. }
  29. public int getHeight()
  30. {
  31. return (int) getMediaBox().height();
  32. }
  33. public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)
  34. {
  35. Matrix matrix = new Matrix();
  36. matrix.postScale(width / getMediaBox().width(), -height / getMediaBox().height());
  37. matrix.postTranslate(0, height);
  38. matrix.postTranslate(-pageSliceBounds.left*width, -pageSliceBounds.top*height);
  39. matrix.postScale(1/pageSliceBounds.width(), 1/pageSliceBounds.height());
  40. return render(new Rect(0,0,width,height), matrix);
  41. }
  42. static PdfPage createPage(long dochandle, int pageno)
  43. {
  44. return new PdfPage(open(dochandle, pageno), dochandle);
  45. }
  46. @Override
  47. protected void finalize() throws Throwable
  48. {
  49. recycle();
  50. super.finalize();
  51. }
  52. public synchronized void recycle() {
  53. if (pageHandle != 0) {
  54. free(pageHandle);
  55. pageHandle = 0;
  56. }
  57. }
  58. private RectF getMediaBox()
  59. {
  60. float[] box = new float[4];
  61. getMediaBox(pageHandle, box);
  62. return new RectF(box[0], box[1], box[2], box[3]);
  63. }
  64. public Bitmap render(Rect viewbox, Matrix matrix)
  65. {
  66. int[] mRect = new int[4];
  67. mRect[0] = viewbox.left;
  68. mRect[1] = viewbox.top;
  69. mRect[2] = viewbox.right;
  70. mRect[3] = viewbox.bottom;
  71. float[] matrixSource = new float[9];
  72. float[] matrixArray = new float[6];
  73. matrix.getValues(matrixSource);
  74. matrixArray[0] = matrixSource[0];
  75. matrixArray[1] = matrixSource[3];
  76. matrixArray[2] = matrixSource[1];
  77. matrixArray[3] = matrixSource[4];
  78. matrixArray[4] = matrixSource[2];
  79. matrixArray[5] = matrixSource[5];
  80. int width = viewbox.width();
  81. int height = viewbox.height();
  82. int[] bufferarray = new int[width * height];
  83. nativeCreateView(docHandle, pageHandle, mRect, matrixArray, bufferarray);
  84. return Bitmap.createBitmap(bufferarray, width, height, Bitmap.Config.RGB_565);
  85. /*ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 2);
  86. render(docHandle, docHandle, mRect, matrixArray, buffer, ByteBuffer.allocateDirect(width * height * 8));
  87. final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  88. bitmap.copyPixelsFromBuffer(buffer);
  89. return bitmap;*/
  90. }
  91. private static native void getMediaBox(long handle, float[] mediabox);
  92. private static native void free(long handle);
  93. private static native long open(long dochandle, int pageno);
  94. private static native void render(long dochandle, long pagehandle,
  95. int[] viewboxarray, float[] matrixarray,
  96. ByteBuffer byteBuffer, ByteBuffer tempBuffer);
  97. private native void nativeCreateView(long dochandle, long pagehandle,
  98. int[] viewboxarray, float[] matrixarray,
  99. int[] bufferarray);
  100. }