]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blobdiff - QRScanner/glass/src/main/java/cz/cvut/fel/dce/qrscanner/pdfviewer/PdfPageView.java
Clean and coment the code.
[hornmich/skoda-qr-demo.git] / QRScanner / glass / src / main / java / cz / cvut / fel / dce / qrscanner / pdfviewer / PdfPageView.java
index ffa8d1c2436fc5b02c6a61564ecea8ceb2aa13c4..8ee3d6e2ed51af8a5a41f56ef3f74cdee4b91f4b 100644 (file)
@@ -4,32 +4,330 @@ import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Paint;
+import android.util.Log;
 import android.view.View;
 
+import java.io.File;
+
+import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
+
 /**
- * Created by michal on 16.2.15.
+ * View for viewing PDF page.
+ *
+ * This view is designed to be used in two ways.
+ * 1) To view the PDF page itself on the device, which is supported by an interface for scrolling, zooming, switching pages and loading pages.
+ * 2) To load the PDF page to a bitmap, which can be accessed in the program by calling getPageBitmap() method.
  */
-public class PdfPageView  extends SceneView {
-       private Bitmap mPdfBitmap;
-       private Paint mBitmapPaint;
+public class PdfPageView  extends View {
+       /**
+        * Value with some allowed minimum and maximum.
+        * @author Michal Horn
+        *
+        * @param <T> some comparable numeric data type
+        */
+       class IntervalValue<T extends Number & Comparable<? super T>> {
+               /**
+                * Stored value
+                */
+               private T mValue;
+               /**
+                * The lowest allowed value. All values stored in the object are
+                * higher or equal this minimum.
+                */
+               private T mMinimum;
+               /**
+                * The highest allowed value. All values stored in the object are
+                * lower or equal this maximum.
+                */
+               private T mMaximum;
+
+               /**
+                * Class constructor.
+                * Creates object that represents a value in range <minimum; maximum>.
+                *
+                * @param defValue      Value to be stored in the object. Must be in the allowed range specified by other two parameters.
+                * @param minimum       The lowes allowed value to be stored in the object.
+                * @param maximum       The highest allowed value to be stored in the object.
+                * @throws Exception Throwed when minimal, maximal or inserted value were rejected.
+                */
+               public IntervalValue(T defValue, T minimum, T maximum) throws Exception {
+                       setMinimum(minimum);
+                       setMaximum(maximum);
+                       if (!setValue(defValue)) {
+                               throw new Exception("Provided value is not in the range.");
+                       }
+               }
+
+               /**
+                * Store value in the object. The value must lie between the minimum and maximum
+                * defined for the object.
+                * @param value value to be stored in the object
+                * @return true - vale was stored successfully,<br>false - value could not be inserted because it was out of range.
+                */
+               public boolean setValue(T value) {
+                       if (value.compareTo(mMinimum) >= 0 && value.compareTo(mMaximum) <= 0) {
+                               mValue = value;
+                               return true;
+                       }
+                       else {
+                               return false;
+                       }
+               }
+
+               /**
+                * Set minimum for values in the object.
+                * Last inserted value is not changed even if it is lower then the new minimum.
+                * @param min new allowed minimum for the values stored to the object.
+                * @throws Exception throwed when new minimum is higher than old maximum.
+                */
+               public void setMinimum(T min) throws Exception {
+                       if (mMaximum != null && mMinimum != null &&  mMinimum.compareTo(mMaximum) > 0) {
+                               throw new Exception("Minimum is higher then maximum.");
+                       }
+                       mMinimum = min;
+               }
+
+               /**
+                * Set maximum for values in the object.
+                * Last inserted value is not changed even if it is higher then the new maximum.
+                * @param max new allowed maximum for the values stored to the object.
+                * @throws Exception throwed when new maximum is lower than old minimum.
+                */
+               public void setMaximum(T max) throws Exception {
+                       if (mMinimum != null && mMaximum != null &&  mMaximum.compareTo(mMinimum) < 0) {
+                               throw new Exception("Maximum is lower then minimum.");
+                       }
+                       mMaximum = max;
+               }
+
+               /**
+                * Get value stored in the object
+                * @return value stored in the object.
+                */
+               public T getValue() {
+                       return mValue;
+               }
+
+               /**
+                * Get the lowest allowed value that can be stored in the object.
+                * @return minimal value
+                */
+               public T getMinimum() {
+                       return mMinimum;
+               }
+
+               /**
+                * Get the highest allowed value that can be stored in the object.
+                * @return maximal value
+                */
+               public T getMaximum() {
+                       return mMaximum;
+               }
+       }
+
+       /**
+        * Interface for events generated by the view
+        * @author Michal Horn
+        *
+        */
+       public interface SceneChange {
+               /**
+                * Invoked every time when the scene should be redrawed.
+                * @param source
+                */
+               public void onViewChanged(PdfPageView source);
+
+               /**
+                * Invoked once the PDF page is loaded and prepared as a bitmap to be shown
+                * @param source
+                */
+               public void onPageLoaded(PdfPageView source);
+       }
+
+       public static final String TAG = "PdfPageView";
+       protected Paint mPaint;
+       protected IntervalValue<Float> mZoom;
+       protected IntervalValue<Float> mXPosition;
+       protected IntervalValue<Float> mYPosition;
+       protected SceneChange mListener;
+       protected float mSceneHeight;
+       protected float mSceneWidth;
+       protected Bitmap mPdfBitmap;
+       protected File mPdfFile;
+       protected Boolean mPageLoaded;
+       private MuPDFCore mPdfCore;
+       private IntervalValue<Integer> mPage;
+       private MuPDFCore.Cookie mPdfCookie;
+
+       public PdfPageView(Context context, String filePath) throws Exception {
+               super(context);
+               mPageLoaded = false;
+               mPdfFile = new File(filePath);
+               if (!mPdfFile.exists()) {
+                       throw new PdfViewerException("File " + filePath + " does not exist.");
+               }
+               mPaint = new Paint();
+               mPdfCore = new MuPDFCore(context, mPdfFile.getAbsolutePath());
+               mPdfCookie = mPdfCore.new Cookie();
+               if (mPdfCore == null) {
+                       throw new PdfViewerException("The PDF file could not be loaded.");
+               }
+               mPage = new IntervalValue<>(0, 0, mPdfCore.countPages()-1);
+               Log.i(TAG, "Pages: " + mPage.toString());
+       }
+
+       /**
+        * Get actual page. The actual page can be selected by setPage() method.
+        * @return actual page.
+        */
+       public int getActualPage() {
+               return mPage.getValue();
+       }
+
+       /**
+        * Get total number of pages in the document
+        * @return total number of pages
+        */
+       public int getLastPage() {
+               return mPage.getMaximum();
+       }
+
+       public boolean setPage(int page) {
+               return mPage.setValue(page);
+       }
+
+       /**
+        * Load PDF and convert selected page to a bitmap.
+        *
+        * The page of the PDF document can be selected by setPage() method.
+        * This method can take significant time to finish. Consider calling it in separated thread.
+        */
+       public void loadPage() {
+               long startTime = System.currentTimeMillis();
+               try {
+                       Log.i(TAG, "Loading PDF page " + getActualPage());
+                       mSceneWidth = mPdfCore.getPageSize(getActualPage()).x;
+                       mSceneHeight = mPdfCore.getPageSize(getActualPage()).y;
+                       Log.i(TAG, "Page size: " + mSceneWidth + "x" + mSceneHeight);
+                       Bitmap.Config mPdfBitmapConf = Bitmap.Config.ARGB_8888;
+                       mPdfBitmap = Bitmap.createBitmap((int)mSceneWidth, (int)mSceneHeight, mPdfBitmapConf);
+                       mPdfCore.drawPage(mPdfBitmap, 0, (int) mSceneWidth, (int) mSceneHeight, 0, 0, (int) mSceneWidth, (int) mSceneHeight, mPdfCookie);
+
+                       mXPosition = new IntervalValue<>(640.0f/2, -mSceneWidth+640.0f/2, mSceneWidth+640.0f/2);
+                       mYPosition = new IntervalValue<>(360.0f/2, -mSceneHeight+360.0f/2, mSceneHeight+360.0f/2);
+                       mZoom = new IntervalValue<>(1.0f, 0.2f, 2.0f);
+               } catch (Exception e) {
+                       Log.e(TAG, "Error in setting page dimensions: " + e.getMessage());
+                       e.printStackTrace();
+               }
+               long endTime = System.currentTimeMillis();
+               mPageLoaded = true;
+               Log.i(TAG, "PDF page loaded in " + Long.toString(endTime - startTime) + "ms.");
+               if (mListener != null) {
+                       mListener.onPageLoaded(this);
+               }
+       }
+
+       /**
+        * Get PDF page bitmap
+        * @return Bitmap of the PDF page or null, if it has not been yet loaded.
+        */
+       public Bitmap getPageBitmap() {
+               return mPdfBitmap;
+       }
+
+       /**
+        * Set a listener of the events
+        * @param listener listener of the events
+        */
+       public void setListener(SceneChange listener) {
+               mListener = listener;
+       }
+
+       /**
+        * Clear listener.
+        */
+       public void clearListener() {
+               mListener = null;
+       }
+
+       /**
+        * Zoom scene function. The scene can be zoomed in or out only in limits
+        * defined in constructor.
+        * @param deltaRatio when > 0 - zoom in,<br> when < 0 - zoom out
+        */
+       public  void zoom(float deltaRatio) {
+               if (!mPageLoaded) return;
+               float ratio = mZoom.getValue();
+               ratio += deltaRatio;
+               if (mZoom.setValue(ratio) && mListener != null) {
+                       mListener.onViewChanged(this);
+               }
+       }
+
+       /**
+        * Move scene function. The scene can be moved only in the rectangle specified
+        * by limits defined in constructor.
+        * @param x change of the x coordinate relatively to the actual position
+        * @param y change of the y coordinate relatively to the actual position
+        */
+       public void move(float x, float y) {
+               if (!mPageLoaded) return;
+               boolean xWasSet = mXPosition.setValue(mXPosition.getValue() + x/mZoom.getValue()*2.2f);
+               boolean yWasSet = mYPosition.setValue(mYPosition.getValue() + y/mZoom.getValue()*2.2f);
+
+               if ((xWasSet || yWasSet) && mListener != null) {
+                       mListener.onViewChanged(this);
+               }
+       }
+       /**
+        * @return actual scene zoom ratio
+        */
+       public float getZoomRatio() {
+               return mZoom.getValue();
+       }
+
+       /**
+        * @return actual scene position on X axis
+        */
+       public float getXPosition() {
+               return mXPosition.getValue();
+       }
+
+       /**
+        * @return actual scene positoin on Y axis
+        */
+       public float getYPosition() {
+               return mYPosition.getValue();
+       }
+
+       /**
+        * @return how tall the scene is
+        */
+       public float getSceneHeight() {
+               return mSceneHeight;
+       }
 
-       public PdfPageView(Context context, Bitmap pdfBitmap, float defZoom, float minZoom, float maxZoom, float defXCenter, float minX, float maxX, float defYCenter, float minY, float maxY) throws Exception {
-               super(context, defZoom, minZoom, maxZoom, defXCenter, minX, maxX, defYCenter, minY, maxY);
-               mPdfBitmap = Bitmap.createBitmap(pdfBitmap);
-               mBitmapPaint = new Paint();
+       /**
+        * @return how wide the scene is
+        */
+       public float getSceneWidth() {
+               return mSceneWidth;
        }
 
        @Override
-       protected void computeSceneDimmensions() {
-               mSceneWidth = mPdfBitmap.getWidth();
-               mSceneHeight = mPdfBitmap.getHeight();
+       protected void onDraw(Canvas c) {
+               if (!mPageLoaded) return;
+               draw(c);
+               super.onDraw(c);
        }
 
        @Override
        public void draw(Canvas c) {
+               if (!mPageLoaded) return;
                c.translate(c.getWidth()/2, c.getHeight()/2);
                c.scale(mZoom.getValue(), mZoom.getValue());
                c.translate(-mXPosition.getValue(), -mYPosition.getValue());
-               c.drawBitmap(mPdfBitmap, 0, 0, mBitmapPaint);
+               c.drawBitmap(mPdfBitmap, 0, 0, mPaint);
        }
 }