]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/commitdiff
Replace PdfViewer with MuPDF, implement preview creating from PDF file
authorMichal Horn <hornmich@fel.cvut.cz>
Tue, 10 Feb 2015 13:43:24 +0000 (14:43 +0100)
committerMichal Horn <hornmich@fel.cvut.cz>
Tue, 10 Feb 2015 13:43:24 +0000 (14:43 +0100)
QRScanner/mobile/build.gradle
QRScanner/mobile/libs/PdfViewer.jar [deleted file]
QRScanner/mobile/libs/iocommons.jar [deleted file]
QRScanner/mobile/mobile.iml
QRScanner/mobile/src/main/AndroidManifest.xml
QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/MainActivity.java
QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
QRScanner/mobile/src/main/res/values/strings.xml
QRScanner/mobile/src/main/res/values/styles.xml

index eb89c047163ab110cb80f05bf268f9a1fcf97256..12b791e82bc0e6c6573d9cc4a562063f8c83c2fb 100644 (file)
@@ -19,9 +19,20 @@ android {
     }
 }
 
+task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') {
+    destinationDir file("$buildDir/native-libs")
+    baseName 'native-libs'
+    extension 'jar'
+    from fileTree(dir: 'libs', include: '**/*.so')
+    into 'lib/'
+}
+
+tasks.withType(JavaCompile) {
+    compileTask -> compileTask.dependsOn(nativeLibsToJar)
+}
+
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:21.0.3'
-    compile files('libs/PdfViewer.jar')
-    compile files('libs/iocommons.jar')
+    compile files('build/native-libs/native-libs.jar')
 }
diff --git a/QRScanner/mobile/libs/PdfViewer.jar b/QRScanner/mobile/libs/PdfViewer.jar
deleted file mode 100644 (file)
index a2404a5..0000000
Binary files a/QRScanner/mobile/libs/PdfViewer.jar and /dev/null differ
diff --git a/QRScanner/mobile/libs/iocommons.jar b/QRScanner/mobile/libs/iocommons.jar
deleted file mode 100644 (file)
index 02ec61e..0000000
Binary files a/QRScanner/mobile/libs/iocommons.jar and /dev/null differ
index 3e24874d85e9a070c30215bc50881c15c65714f3..d81bd082513ded60e0e4cbc6df0b41bf4f3b1925 100644 (file)
@@ -80,6 +80,7 @@
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
       <excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
+      <excludeFolder url="file://$MODULE_DIR$/build/native-libs" />
       <excludeFolder url="file://$MODULE_DIR$/build/outputs" />
       <excludeFolder url="file://$MODULE_DIR$/build/tmp" />
     </content>
@@ -87,6 +88,7 @@
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="library" exported="" name="appcompat-v7-21.0.3" level="project" />
     <orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
+    <orderEntry type="library" exported="" name="native-libs" level="project" />
     <orderEntry type="library" exported="" name="iocommons" level="project" />
     <orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
     <orderEntry type="library" exported="" name="PdfViewer" level="project" />
index 1144179f3956b32ea7c256bce6b80a88614ffa77..c466fe32434a0e24acf65fb6eb39e02104549081 100644 (file)
             android:name=".PreviewActivity"
             android:label="@string/title_activity_preview" >
         </activity>
+        <activity
+        android:name="cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity"
+        android:label="@string/app_name"> // android:theme="@style/AppBaseTheme"
+        <intent-filter>
+            <action android:name="android.intent.action.VIEW" />
+
+            <category android:name="android.intent.category.DEFAULT" />
+
+            <data android:mimeType="application/vnd.ms-xpsdocument" />
+        </intent-filter>
+        <intent-filter>
+            <action android:name="android.intent.action.VIEW" />
+
+            <category android:name="android.intent.category.DEFAULT" />
+
+            <data android:mimeType="application/pdf" />
+        </intent-filter>
+        <intent-filter>
+            <action android:name="android.intent.action.VIEW" />
+
+            <category android:name="android.intent.category.DEFAULT" />
+
+            <data android:mimeType="application/x-cbz" />
+        </intent-filter>
+        <intent-filter>
+            <action android:name="android.intent.action.VIEW" />
+
+            <category android:name="android.intent.category.DEFAULT" />
+            <category android:name="android.intent.category.BROWSABLE" />
+
+            <data android:scheme="file" />
+            <data android:mimeType="*/*" />
+            <data android:pathPattern=".*\\.xps" />
+            <data android:host="*" />
+        </intent-filter>
+        <intent-filter>
+            <action android:name="android.intent.action.VIEW" />
+
+            <category android:name="android.intent.category.DEFAULT" />
+            <category android:name="android.intent.category.BROWSABLE" />
+
+            <data android:scheme="file" />
+            <data android:mimeType="*/*" />
+            <data android:pathPattern=".*\\.pdf" />
+            <data android:host="*" />
+        </intent-filter>
+        <intent-filter>
+            <action android:name="android.intent.action.VIEW" />
+
+            <category android:name="android.intent.category.DEFAULT" />
+            <category android:name="android.intent.category.BROWSABLE" />
+
+            <data android:scheme="file" />
+            <data android:mimeType="*/*" />
+            <data android:pathPattern=".*\\.cbz" />
+            <data android:host="*" />
+        </intent-filter>
+    </activity>
     </application>
 
 </manifest>
index a176ba9d28cf42c2c05fad7aef1025da74e1d406..67857332bbb9f78afe917d37a72863194b9bc003 100644 (file)
@@ -1,27 +1,12 @@
 package cz.cvut.fel.dce.qrscanner;
 
-import android.content.Context;
 import android.content.Intent;
-import android.content.res.AssetManager;
-import android.graphics.Bitmap;
-import android.graphics.RectF;
-import android.support.annotation.Nullable;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
 
-import com.sun.pdfview.PDFFile;
-import com.sun.pdfview.PDFPage;
-
-import net.sf.andpdf.nio.ByteBuffer;
-
-import org.apache.commons.io.IOUtils;
-
-import java.io.IOException;
-import java.io.InputStream;
-
 import cz.cvut.fel.dce.qrscanner.integration.IntentIntegrator;
 import cz.cvut.fel.dce.qrscanner.integration.IntentResult;
 
index 88e4cb4bed8eca6ae7dde5c9e49d3a61967cfdb1..79ee599bf9cafbc4b408a962787cfad06b1f7b27 100644 (file)
@@ -1,63 +1,48 @@
 package cz.cvut.fel.dce.qrscanner;
 
-import android.content.Context;
 import android.content.Intent;
-import android.content.res.AssetManager;
 import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.graphics.RectF;
-import android.net.Uri;
-import android.os.Environment;
-import android.support.annotation.Nullable;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
+import android.view.ViewTreeObserver;
 import android.widget.ImageView;
 import android.widget.Toast;
 
-import com.sun.pdfview.PDFFile;
-import com.sun.pdfview.PDFPage;
-
-import net.sf.andpdf.nio.ByteBuffer;
-
-import org.apache.commons.io.IOUtils;
-
+import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
 
-
-public class PreviewActivity extends ActionBarActivity {
+public class PreviewActivity extends ActionBarActivity implements ViewTreeObserver.OnGlobalLayoutListener {
        public static final String TAG = "PreviewActivity";
+       public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
        public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
-       public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.png";
+       public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
+
+       private ImageView mPreviewImg;
+       private ViewTreeObserver mPreviewImgObserver;
+       private String mComponentId;
+       private String mComponentRootPath;
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_preview);
+               mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
+               mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
+               Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
+               mPreviewImgObserver.addOnGlobalLayoutListener(this);
+
                Intent intent = getIntent();
-               String comp_id = intent.getStringExtra("COMPONENT_ID");
-               if (comp_id != null) {
-                       Log.i(TAG, "Received component id: " + comp_id);
-                       String path = "/storage/sdcard0/Pictures" + SKODA_DOCS_PATH_EXTENSION + comp_id + "/";
-                       String picturePath = path + SKODA_COMP_PICTURE_NAME;
-                       Log.i(TAG, "Path to files: " + picturePath);
-                       Bitmap previewBitmap = new BitmapFactory().decodeFile(picturePath);
-                       if (previewBitmap != null) {
-                               ImageView previewImg = (ImageView) findViewById(R.id.imgComponent);
-                               previewImg.setImageBitmap(previewBitmap);
-                               Uri uri = Uri.parse(path + "Bild_Arbeitseinleitung.pdf");
-                               Intent pdfIntent = new Intent(this,MuPDFActivity.class);
-                               pdfIntent.setAction(Intent.ACTION_VIEW);
-                               pdfIntent.setData(uri);
-                               startActivity(intent);
-                       }
-                       else {
-                               Toast toast = Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_LONG);
+               mComponentId = intent.getStringExtra("COMPONENT_ID");
+
+               if (mComponentId != null) {
+                       Log.i(TAG, "Received component id: " + mComponentId);
+                       mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
+                       File rootPath = new File(mComponentRootPath);
+                       if (!rootPath.isDirectory()) {
+                               Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
                                toast.show();
                                finish();
                        }
@@ -68,6 +53,14 @@ public class PreviewActivity extends ActionBarActivity {
                }
        }
 
+       @Override
+       protected void onDestroy() {
+               super.onDestroy();
+               Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
+               if (mPreviewImgObserver.isAlive()) {
+                       mPreviewImgObserver.removeOnGlobalLayoutListener(this);
+               }
+       }
 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
@@ -91,70 +84,29 @@ public class PreviewActivity extends ActionBarActivity {
                return super.onOptionsItemSelected(item);
        }
 
-       /**
-        * Use this to load a pdf file from your assets and render it to a Bitmap.
-        *
-        * @param context
-        *            current context.
-        * @param filePath
-        *            of the pdf file in the assets.
-        * @return a bitmap.
-        */
-       @Nullable
-       private static Bitmap renderToBitmap(Context context, String filePath) {
-               Bitmap bi = null;
-               InputStream inStream = null;
-               try {
-                       Log.d(TAG, "Attempting to copy this file: " + filePath);
-                       inStream = new FileInputStream(new File(filePath));
-                       bi = renderToBitmap(context, inStream);
-               } catch (IOException e) {
-                       e.printStackTrace();
-               } finally {
+       @Override
+       public void onGlobalLayout() {
+               if (mPreviewImg != null) {
                        try {
-                               if (inStream != null) {
-                                       inStream.close();
-                               }
-                       } catch (IOException e) {
-                               // do nothing because the stream has already been closed
-                       }
-               }
-               return bi;
-       }
+                               String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
+                               Log.i(TAG, "Path to component files: " + picturePath);
 
-       /**
-        * Use this to render a pdf file given as InputStream to a Bitmap.
-        *
-        * @param context
-        *            current context.
-        * @param inStream
-        *            the inputStream of the pdf file.
-        * @return a bitmap.
-        */
-       @Nullable
-       private static Bitmap renderToBitmap(Context context, InputStream inStream) {
-               Bitmap bi = null;
-               try {
-                       byte[] decode = IOUtils.toByteArray(inStream);
-                       ByteBuffer buf = ByteBuffer.wrap(decode);
-                       PDFFile pdfFile = new PDFFile(buf);
-                       int numPages = pdfFile.getNumPages();
-                       Log.i(TAG, "Num pages: " + Integer.toString(numPages));
-                       PDFPage mPdfPage = new PDFFile(buf).getPage(0);
-                       float width = mPdfPage.getWidth();
-                       float height = mPdfPage.getHeight();
-                       RectF clip = null;
-                       bi = mPdfPage.getImage((int) (width), (int) (height), clip, false,
-                                       true);
-               } catch (IOException e) {
-                       e.printStackTrace();
-               } finally {
-                       try {
-                               inStream.close();
-                       } catch (IOException e) {
-                               // do nothing because the stream has already been closed
+                               MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
+                               MuPDFCore.Cookie cookie = core.new Cookie();
+                               int previewW = mPreviewImg.getWidth();
+                               int previewH = mPreviewImg.getHeight();
+                               Bitmap.Config conf = Bitmap.Config.ARGB_8888;
+                               Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
+                               core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
+                               mPreviewImg.setImageBitmap(previewBitmap);
+                       } catch (Exception e) {
+                               Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
+                               toast.show();
+                               e.printStackTrace();
                        }
                }
-               return bi;
+               else {
+                       Log.e(TAG, "ImageView for preview image could not be found in the resources.");
+               }
        }
 }
index f01f11146eb0b2c6a13930e5c3773346dc4103cf..e3bf5fe7766671019897f64882953a92be2847cf 100644 (file)
@@ -6,5 +6,57 @@
     <string name="action_settings">Settings</string>
     <string name="start_scan">Scan</string>
     <string name="title_activity_preview">PreviewActivity</string>
+       <string name="version">1.6 (git build)</string>
+       <string name="no_media_warning">Storage media not present</string>
+       <string name="no_media_hint">Sharing the storage media with a PC can make it inaccessible</string>
+       <string name="cancel">Cancel</string>
+       <string name="search_backwards">Search backwards</string>
+       <string name="search_forwards">Search forwards</string>
+       <string name="search_document">Search document</string>
+       <string name="picker_title_App_Ver_Dir">%1$s %2$s: %3$s</string>
+       <string name="outline_title">Table of Contents</string>
+       <string name="enter_password">Enter password</string>
+       <string name="text_not_found">Text not found</string>
+       <string name="searching_">Searching&#8230;</string>
+       <string name="toggle_links">Highlight and enable links</string>
+       <string name="no_further_occurrences_found">No further occurrences found</string>
+       <string name="select">Select</string>
+       <string name="search">Search</string>
+       <string name="copy">Copy</string>
+       <string name="strike_out">Strike-out</string>
+       <string name="delete">Delete</string>
+       <string name="highlight">Highlight</string>
+       <string name="underline">Underline</string>
+       <string name="edit_annotations">Edit annotations</string>
+       <string name="ink">Ink</string>
+       <string name="save">Save</string>
+       <string name="print">Print</string>
+       <string name="dismiss">Dismiss</string>
+       <string name="parent_directory">[Up one level]</string>
+       <string name="yes">Yes</string>
+       <string name="no">No</string>
+       <string name="entering_reflow_mode">Entering reflow mode</string>
+       <string name="leaving_reflow_mode">Leaving reflow mode</string>
+       <string name="print_failed">Print failed</string>
+       <string name="select_text">Select text</string>
+       <string name="copied_to_clipboard">Copied to clipboard</string>
+       <string name="no_text_selected">No text selected</string>
+       <string name="draw_annotation">Draw annotation</string>
+       <string name="nothing_to_save">Nothing to save</string>
+       <string name="document_has_changes_save_them_">Document has changes. Save them?</string>
+       <string name="cannot_open_document">Cannot open document</string>
+       <string name="cannot_open_document_Reason">Cannot open document: %1$s</string>
+       <string name="cannot_open_file_Path">Cannot open file: %1$s</string>
+       <string name="cannot_open_buffer">Cannot open buffer</string>
+       <string name="fill_out_text_field">Fill out text field</string>
+       <string name="okay">Okay</string>
+       <string name="choose_value">Choose value</string>
+       <string name="not_supported">Not supported</string>
+       <string name="copy_text_to_the_clipboard">Copy text to the clipboard</string>
+       <string name="more">More</string>
+       <string name="accept">Accept</string>
+       <string name="copy_text">copy text</string>
+       <string name="format_currently_not_supported">Format currently not supported</string>
+       <string name="toggle_reflow_mode">Toggle reflow mode</string>
 
 </resources>
index 766ab9930487aebdf634594950b22d39b44d8125..3ce4cbe2a1d1dc0a2091617dab3e770a3e228d7d 100644 (file)
@@ -4,5 +4,7 @@
     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
         <!-- Customize your theme here. -->
     </style>
-
+    <style name="AppBaseTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
+        <item name="android:windowBackground">@drawable/tiled_background</item>
+   </style>
 </resources>