OSDN Git Service

ライブビューができるようになった。
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / camtest / CamTest.java
1 package net.osdn.gokigen.cameratest.camtest;
2
3 import android.app.Activity;
4 import android.graphics.Bitmap;
5 import android.graphics.BitmapFactory;
6 import android.os.Environment;
7 import android.util.Log;
8 import android.view.View;
9 import android.widget.ImageView;
10 import android.widget.TextView;
11
12 import com.google.android.material.snackbar.Snackbar;
13
14 import net.osdn.gokigen.cameratest.R;
15 import net.osdn.gokigen.cameratest.fuji.Connection;
16 import net.osdn.gokigen.cameratest.fuji.ILiveViewImage;
17 import net.osdn.gokigen.cameratest.fuji.ReceivedDataHolder;
18
19 import androidx.annotation.NonNull;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24
25
26 public class CamTest implements View.OnClickListener, ILiveViewImage
27 {
28     private String TAG = toString();
29     private final Activity activity;
30     private TextView textview;
31     private Connection connection;
32     private FileOutputStream outputStream = null;
33     private int offsetSize = 18;  // 4byte: データサイズ、14byte: (謎の)ヘッダ
34
35     public CamTest(@NonNull Activity activity)
36     {
37         this.activity = activity;
38         this.connection = new Connection(this);
39     }
40
41     public void connect()
42     {
43         Log.v(TAG, "connect request");
44         try
45         {
46             prepareFile();
47
48             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
49
50             showMessageText("START CONNECT");
51             Thread thread = new Thread(new Runnable() {
52                 @Override
53                 public void run() {
54                     boolean ret = connection.start_connect();
55                     if (!ret)
56                     {
57                         showMessageText("CONNECT FAILURE...");
58                     }
59                 }
60             });
61             thread.start();
62         }
63         catch (Exception e)
64         {
65             e.printStackTrace();
66         }
67     }
68
69     public void settings()
70     {
71         Log.v(TAG, "settings menu");
72
73         showMessageText("BBBB");
74     }
75
76     public void valueUp()
77     {
78         Log.v(TAG, "value UP");
79         offsetSize++;
80         showMessageText("OFFSET : " + offsetSize);
81     }
82
83     public void valueDown()
84     {
85         Log.v(TAG, "value DOWN");
86         offsetSize--;
87         showMessageText("OFFSET : " + offsetSize);
88     }
89
90
91     private void showMessageText(final String message)
92     {
93         activity.runOnUiThread(new Runnable() {
94             @Override
95             public void run() {
96                 try {
97                     if (textview == null) {
98                         textview = activity.findViewById(R.id.show_information);
99                     }
100                     if (textview != null) {
101                         textview.setText(message);
102                     }
103                 } catch (Exception e) {
104                     e.printStackTrace();
105                 }
106             }
107         });
108     }
109
110     @Override
111     public void onClick(View v)
112     {
113         Log.v(TAG, "onClick : " + v.getId());
114         int id = v.getId();
115         switch (id)
116         {
117             case R.id.button1:
118                 doShutter();
119                 break;
120             case R.id.button2:
121                 readImageFile("sampledata1.bin");
122                 showMessageText("show 'sampledata1.bin'.");
123                 break;
124             case R.id.button3:
125                 readImageFile("sampledata2.bin");
126                 showMessageText("show 'sampledata2.bin'.");
127                 break;
128             case R.id.button4:
129                 readImageFile("sampledata3.bin");
130                 showMessageText("show 'sampledata3.bin'.");
131                 break;
132             default:
133                 showMessageText("Unknown : " + id);
134                 break;
135         }
136     }
137
138     private void doShutter()
139     {
140         Log.v(TAG, "execute shutter");
141         try
142         {
143             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.shutter, Snackbar.LENGTH_SHORT).show();
144             showMessageText("SHUTTER");
145             Thread thread = new Thread(new Runnable() {
146                 @Override
147                 public void run() {
148                     boolean ret = connection.execute_shutter();
149                     if (!ret)
150                     {
151                         showMessageText("SHUTTER FAILURE...");
152                     }
153                 }
154             });
155             thread.start();
156         }
157         catch (Exception e)
158         {
159             e.printStackTrace();
160         }
161     }
162
163     @Override
164     public void updateImage(ReceivedDataHolder receivedData)
165     {
166         try
167         {
168             final byte[] dataValue = receivedData.getData();
169             //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
170             //byte[] endJpegMarker   = {(byte)0xff, (byte)0xd9};
171
172             Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
173
174             // ダミーの記録ファイルが開いていたらファイルに書いておく。
175             outputFile(receivedData);
176
177             ///////  Bitmap画像を作る... //////
178             final Bitmap imageData = getBitmap(receivedData);
179             if (imageData != null)
180             {
181                 //////  画像を更新する
182                 activity.runOnUiThread(new Runnable() {
183                     @Override
184                     public void run() {
185                         try {
186                             // ビットマップイメージを表示する。
187                             ImageView view = activity.findViewById(R.id.imageView);
188                             view.setImageBitmap(imageData);
189                             view.invalidate();
190                         } catch (Throwable e) {
191                             e.printStackTrace();
192                         }
193                     }
194                 });
195             }
196         }
197         catch (Throwable e)
198         {
199             e.printStackTrace();
200         }
201     }
202
203     @Override
204     public void updateImage(final Bitmap bitmap)
205     {
206         try
207         {
208             Log.v(TAG, "bitmap : " + bitmap.getByteCount() + " bytes.");
209
210             //////  画像を更新する
211             activity.runOnUiThread(new Runnable() {
212                 @Override
213                 public void run() {
214                     try
215                     {
216                         // ビットマップイメージを表示する。
217                         ImageView view = activity.findViewById(R.id.imageView);
218                         view.setImageBitmap(bitmap);
219                         view.invalidate();
220                     }
221                     catch (Throwable e)
222                     {
223                         e.printStackTrace();
224                     }
225                 }
226             });
227         }
228         catch (Throwable e)
229         {
230             e.printStackTrace();
231         }
232     }
233
234     private void outputFile(ReceivedDataHolder receivedData)
235     {
236         try
237         {
238             if (outputStream != null)
239             {
240                 final byte[] byteData = receivedData.getData();
241                 outputStream.write(byteData, 0, byteData.length);
242             }
243         }
244         catch (Exception e)
245         {
246             e.printStackTrace();
247         }
248     }
249
250     private void prepareFile()
251     {
252         try
253         {
254             final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/AirA01a/";
255             final String outputFileName = "camtest.bin";
256             File filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase());
257             outputStream = new FileOutputStream(filepath);
258           }
259         catch (Exception e)
260         {
261             e.printStackTrace();
262             outputStream = null;
263         }
264     }
265
266     private void readImageFile(final String readFileName)
267     {
268         Thread thread = new Thread(new Runnable() {
269             @Override
270             public void run() {
271                     readImageFileImpl(readFileName);
272             }
273         });
274         try
275         {
276             thread.start();
277         }
278         catch (Exception e)
279         {
280             e.printStackTrace();
281         }
282     }
283
284     private void readImageFileImpl(final String readFileName)
285     {
286         try
287         {
288             Log.v(TAG, "readImageFileImpl() : " + readFileName);
289             final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/AirA01a/";
290             File filepath = new File(directoryPath.toLowerCase(), readFileName.toLowerCase());
291             FileInputStream istr = new FileInputStream(filepath);
292             final Bitmap imageData = BitmapFactory.decodeStream(istr);
293             istr.close();
294             if (imageData == null)
295             {
296                 Log.v(TAG, "readImageFileImpl() : bitmap is NULL.");
297             }
298             else
299             {
300                 Log.v(TAG, "readImageFileImpl() : bitmap is " + imageData.getByteCount() + " bytes.");
301             }
302
303             //////  画像表示を更新する //////
304             activity.runOnUiThread(new Runnable() {
305                 @Override
306                 public void run() {
307                     try
308                     {
309                         // ビットマップイメージを表示する。
310                         ImageView view = activity.findViewById(R.id.information_view);
311                         view.setImageBitmap(imageData);
312                         view.invalidate();
313                     }
314                     catch (Throwable e)
315                     {
316                         e.printStackTrace();
317                     }
318                 }
319             });
320         }
321         catch (Throwable e)
322         {
323             e.printStackTrace();
324         }
325     }
326
327
328     private Bitmap getBitmap(ReceivedDataHolder receivedData)
329     {
330         try
331         {
332             final byte[] data = receivedData.getData();
333             final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
334             if (imageData == null)
335             {
336                 Log.v(TAG, "readImageFileImpl() : bitmap is NULL. (offset : " + offsetSize + ")");
337             }
338             else
339             {
340                 Log.v(TAG, "readImageFileImpl() : bitmap is " + imageData.getByteCount() + " bytes.");
341             }
342             return (imageData);
343         }
344         catch (Exception e)
345         {
346             e.printStackTrace();
347         }
348         return (null);
349     }
350 }