]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
88e4cb4bed8eca6ae7dde5c9e49d3a61967cfdb1
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / src / main / java / cz / cvut / fel / dce / qrscanner / PreviewActivity.java
1 package cz.cvut.fel.dce.qrscanner;
2
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.res.AssetManager;
6 import android.graphics.Bitmap;
7 import android.graphics.BitmapFactory;
8 import android.graphics.RectF;
9 import android.net.Uri;
10 import android.os.Environment;
11 import android.support.annotation.Nullable;
12 import android.support.v7.app.ActionBarActivity;
13 import android.os.Bundle;
14 import android.util.Log;
15 import android.view.Menu;
16 import android.view.MenuItem;
17 import android.widget.ImageView;
18 import android.widget.Toast;
19
20 import com.sun.pdfview.PDFFile;
21 import com.sun.pdfview.PDFPage;
22
23 import net.sf.andpdf.nio.ByteBuffer;
24
25 import org.apache.commons.io.IOUtils;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32
33 public class PreviewActivity extends ActionBarActivity {
34         public static final String TAG = "PreviewActivity";
35         public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
36         public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.png";
37
38         @Override
39         protected void onCreate(Bundle savedInstanceState) {
40                 super.onCreate(savedInstanceState);
41                 setContentView(R.layout.activity_preview);
42                 Intent intent = getIntent();
43                 String comp_id = intent.getStringExtra("COMPONENT_ID");
44                 if (comp_id != null) {
45                         Log.i(TAG, "Received component id: " + comp_id);
46                         String path = "/storage/sdcard0/Pictures" + SKODA_DOCS_PATH_EXTENSION + comp_id + "/";
47                         String picturePath = path + SKODA_COMP_PICTURE_NAME;
48                         Log.i(TAG, "Path to files: " + picturePath);
49                         Bitmap previewBitmap = new BitmapFactory().decodeFile(picturePath);
50                         if (previewBitmap != null) {
51                                 ImageView previewImg = (ImageView) findViewById(R.id.imgComponent);
52                                 previewImg.setImageBitmap(previewBitmap);
53                                 Uri uri = Uri.parse(path + "Bild_Arbeitseinleitung.pdf");
54                                 Intent pdfIntent = new Intent(this,MuPDFActivity.class);
55                                 pdfIntent.setAction(Intent.ACTION_VIEW);
56                                 pdfIntent.setData(uri);
57                                 startActivity(intent);
58                         }
59                         else {
60                                 Toast toast = Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_LONG);
61                                 toast.show();
62                                 finish();
63                         }
64                 }
65                 else {
66                         Log.i(TAG, "No component id received");
67                         finish();
68                 }
69         }
70
71
72         @Override
73         public boolean onCreateOptionsMenu(Menu menu) {
74                 // Inflate the menu; this adds items to the action bar if it is present.
75                 getMenuInflater().inflate(R.menu.menu_preview, menu);
76                 return true;
77         }
78
79         @Override
80         public boolean onOptionsItemSelected(MenuItem item) {
81                 // Handle action bar item clicks here. The action bar will
82                 // automatically handle clicks on the Home/Up button, so long
83                 // as you specify a parent activity in AndroidManifest.xml.
84                 int id = item.getItemId();
85
86                 //noinspection SimplifiableIfStatement
87                 if (id == R.id.action_settings) {
88                         return true;
89                 }
90
91                 return super.onOptionsItemSelected(item);
92         }
93
94         /**
95          * Use this to load a pdf file from your assets and render it to a Bitmap.
96          *
97          * @param context
98          *            current context.
99          * @param filePath
100          *            of the pdf file in the assets.
101          * @return a bitmap.
102          */
103         @Nullable
104         private static Bitmap renderToBitmap(Context context, String filePath) {
105                 Bitmap bi = null;
106                 InputStream inStream = null;
107                 try {
108                         Log.d(TAG, "Attempting to copy this file: " + filePath);
109                         inStream = new FileInputStream(new File(filePath));
110                         bi = renderToBitmap(context, inStream);
111                 } catch (IOException e) {
112                         e.printStackTrace();
113                 } finally {
114                         try {
115                                 if (inStream != null) {
116                                         inStream.close();
117                                 }
118                         } catch (IOException e) {
119                                 // do nothing because the stream has already been closed
120                         }
121                 }
122                 return bi;
123         }
124
125         /**
126          * Use this to render a pdf file given as InputStream to a Bitmap.
127          *
128          * @param context
129          *            current context.
130          * @param inStream
131          *            the inputStream of the pdf file.
132          * @return a bitmap.
133          */
134         @Nullable
135         private static Bitmap renderToBitmap(Context context, InputStream inStream) {
136                 Bitmap bi = null;
137                 try {
138                         byte[] decode = IOUtils.toByteArray(inStream);
139                         ByteBuffer buf = ByteBuffer.wrap(decode);
140                         PDFFile pdfFile = new PDFFile(buf);
141                         int numPages = pdfFile.getNumPages();
142                         Log.i(TAG, "Num pages: " + Integer.toString(numPages));
143                         PDFPage mPdfPage = new PDFFile(buf).getPage(0);
144                         float width = mPdfPage.getWidth();
145                         float height = mPdfPage.getHeight();
146                         RectF clip = null;
147                         bi = mPdfPage.getImage((int) (width), (int) (height), clip, false,
148                                         true);
149                 } catch (IOException e) {
150                         e.printStackTrace();
151                 } finally {
152                         try {
153                                 inStream.close();
154                         } catch (IOException e) {
155                                 // do nothing because the stream has already been closed
156                         }
157                 }
158                 return bi;
159         }
160 }