OSDN Git Service

AF駆動、AFlock解除のコマンドを追加。(調整中)
[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.graphics.PointF;
7 import android.os.Environment;
8 import android.util.Log;
9 import android.view.MotionEvent;
10 import android.view.View;
11 import android.widget.ImageView;
12 import android.widget.TextView;
13
14 import com.google.android.material.snackbar.Snackbar;
15
16 import net.osdn.gokigen.cameratest.R;
17 import net.osdn.gokigen.cameratest.fuji.Connection;
18 import net.osdn.gokigen.cameratest.fuji.ILiveViewImage;
19 import net.osdn.gokigen.cameratest.fuji.ReceivedDataHolder;
20
21 import androidx.annotation.NonNull;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25
26
27 public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiveViewImage
28 {
29     private String TAG = toString();
30     private final Activity activity;
31     private TextView textview;
32     private Connection connection;
33     //private FileOutputStream outputStream = null;
34     private int offsetSize = 18;  // 4byte: データサイズ、14byte: (謎の)ヘッダ
35
36     public CamTest(@NonNull Activity activity)
37     {
38         this.activity = activity;
39         this.connection = new Connection(this);
40     }
41
42     public void connect()
43     {
44         Log.v(TAG, "connect request");
45         try
46         {
47             //prepareFile();
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 disconnect()
70     {
71         try
72         {
73             connection.disconnect();
74         }
75         catch (Exception e)
76         {
77             e.printStackTrace();
78         }
79     }
80
81     public void resetConnection()
82     {
83         Log.v(TAG, "Reset Connection");
84
85         showMessageText("RESET CONNECTION");
86         try
87         {
88             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_reset, Snackbar.LENGTH_SHORT).show();
89             Thread thread = new Thread(new Runnable() {
90                 @Override
91                 public void run() {
92                     connection.reset_to_camera();
93                 }
94             });
95             thread.start();
96         }
97         catch (Exception e)
98         {
99             e.printStackTrace();
100         }
101
102     }
103 /*
104     public void settings()
105     {
106         Log.v(TAG, "settings menu");
107
108         showMessageText("BBBB");
109     }
110
111     public void valueUp()
112     {
113         Log.v(TAG, "value UP");
114         offsetSize++;
115         showMessageText("OFFSET : " + offsetSize);
116     }
117
118     public void valueDown()
119     {
120         Log.v(TAG, "value DOWN");
121         offsetSize--;
122         showMessageText("OFFSET : " + offsetSize);
123     }
124 */
125
126     private void showMessageText(final String message)
127     {
128         activity.runOnUiThread(new Runnable() {
129             @Override
130             public void run() {
131                 try {
132                     if (textview == null) {
133                         textview = activity.findViewById(R.id.show_information);
134                     }
135                     if (textview != null) {
136                         textview.setText(message);
137                     }
138                 } catch (Exception e) {
139                     e.printStackTrace();
140                 }
141             }
142         });
143     }
144
145     @Override
146     public void onClick(View v)
147     {
148         Log.v(TAG, "onClick : " + v.getId());
149         int id = v.getId();
150         switch (id)
151         {
152             case R.id.button1:
153                 doShutter();
154                 break;
155             case R.id.button2:
156                 unlockFocus();
157                 break;
158             case R.id.button3:
159                 //readImageFile("sampledata2.bin");
160                 //showMessageText("show 'sampledata2.bin'.");
161                 break;
162             case R.id.button4:
163                 //readImageFile("sampledata3.bin");
164                 //showMessageText("show 'sampledata3.bin'.");
165                 break;
166             default:
167                 showMessageText("Unknown : " + id);
168                 break;
169         }
170     }
171
172     private void doShutter()
173     {
174         Log.v(TAG, "execute shutter");
175         try
176         {
177             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.shutter, Snackbar.LENGTH_SHORT).show();
178             showMessageText("SHUTTER");
179             Thread thread = new Thread(new Runnable() {
180                 @Override
181                 public void run() {
182                     boolean ret = connection.execute_shutter();
183                     if (!ret)
184                     {
185                         showMessageText("SHUTTER FAILURE...");
186                     }
187                 }
188             });
189             thread.start();
190         }
191         catch (Exception e)
192         {
193             e.printStackTrace();
194         }
195     }
196
197     private void driveAutoFocus(final PointF point)
198     {
199         if (point == null)
200         {
201             return;
202         }
203         try
204         {
205             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.drive_af, Snackbar.LENGTH_SHORT).show();
206             showMessageText("AF : " + point.x + "," + point.y);
207             Thread thread = new Thread(new Runnable() {
208                 @Override
209                 public void run() {
210                     boolean ret = connection.execute_focus_point(point);
211                     if (!ret)
212                     {
213                         showMessageText("Auto Focus FAILURE...");
214                     }
215                 }
216             });
217             thread.start();
218         }
219         catch (Exception e)
220         {
221             e.printStackTrace();
222         }
223     }
224     private void unlockFocus()
225     {
226         try
227         {
228             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.unlock_focus, Snackbar.LENGTH_SHORT).show();
229             Thread thread = new Thread(new Runnable() {
230                 @Override
231                 public void run() {
232                     boolean ret = connection.execute_unlock_focus();
233                     if (!ret)
234                     {
235                         showMessageText("Unlock Focus FAILURE...");
236                     }
237                 }
238             });
239             thread.start();
240         }
241         catch (Exception e)
242         {
243             e.printStackTrace();
244         }
245     }
246
247     @Override
248     public void updateImage(ReceivedDataHolder receivedData)
249     {
250         try
251         {
252             final byte[] dataValue = receivedData.getData();
253             //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
254             //byte[] endJpegMarker   = {(byte)0xff, (byte)0xd9};
255
256             Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
257
258             // ダミーの記録ファイルが開いていたらファイルに書いておく。
259             //outputFile(receivedData);
260
261             ///////  Bitmap画像を作る... //////
262             final Bitmap imageData = getBitmap(receivedData);
263             if (imageData != null)
264             {
265                 //////  画像を更新する
266                 activity.runOnUiThread(new Runnable() {
267                     @Override
268                     public void run() {
269                         try {
270                             // ビットマップイメージを表示する。
271                             ImageView view = activity.findViewById(R.id.imageView);
272                             view.setImageBitmap(imageData);
273                             view.invalidate();
274                         } catch (Throwable e) {
275                             e.printStackTrace();
276                         }
277                     }
278                 });
279             }
280         }
281         catch (Throwable e)
282         {
283             e.printStackTrace();
284         }
285     }
286
287     @Override
288     public void updateImage(final Bitmap bitmap)
289     {
290         try
291         {
292             Log.v(TAG, "bitmap : " + bitmap.getByteCount() + " bytes.");
293
294             //////  画像を更新する
295             activity.runOnUiThread(new Runnable() {
296                 @Override
297                 public void run() {
298                     try
299                     {
300                         // ビットマップイメージを表示する。
301                         ImageView view = activity.findViewById(R.id.imageView);
302                         view.setImageBitmap(bitmap);
303                         view.invalidate();
304                     }
305                     catch (Throwable e)
306                     {
307                         e.printStackTrace();
308                     }
309                 }
310             });
311         }
312         catch (Throwable e)
313         {
314             e.printStackTrace();
315         }
316     }
317 /*
318     private void outputFile(ReceivedDataHolder receivedData)
319     {
320         try
321         {
322             if (outputStream != null)
323             {
324                 final byte[] byteData = receivedData.getData();
325                 outputStream.write(byteData, 0, byteData.length);
326             }
327         }
328         catch (Exception e)
329         {
330             e.printStackTrace();
331         }
332     }
333
334     private void prepareFile()
335     {
336         try
337         {
338             final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/AirA01a/";
339             final String outputFileName = "camtest.bin";
340             File filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase());
341             outputStream = new FileOutputStream(filepath);
342           }
343         catch (Exception e)
344         {
345             e.printStackTrace();
346             outputStream = null;
347         }
348     }
349
350     private void readImageFile(final String readFileName)
351     {
352         Thread thread = new Thread(new Runnable() {
353             @Override
354             public void run() {
355                     readImageFileImpl(readFileName);
356             }
357         });
358         try
359         {
360             thread.start();
361         }
362         catch (Exception e)
363         {
364             e.printStackTrace();
365         }
366     }
367
368     private void readImageFileImpl(final String readFileName)
369     {
370         try
371         {
372             Log.v(TAG, "readImageFileImpl() : " + readFileName);
373             final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/AirA01a/";
374             File filepath = new File(directoryPath.toLowerCase(), readFileName.toLowerCase());
375             FileInputStream istr = new FileInputStream(filepath);
376             final Bitmap imageData = BitmapFactory.decodeStream(istr);
377             istr.close();
378             if (imageData == null)
379             {
380                 Log.v(TAG, "readImageFileImpl() : bitmap is NULL.");
381             }
382             else
383             {
384                 Log.v(TAG, "readImageFileImpl() : bitmap is " + imageData.getByteCount() + " bytes.");
385             }
386             //////  画像表示を更新する //////
387             activity.runOnUiThread(new Runnable() {
388                 @Override
389                 public void run() {
390                     try
391                     {
392                         // ビットマップイメージを表示する。
393                         ImageView view = activity.findViewById(R.id.information_view);
394                         view.setImageBitmap(imageData);
395                         view.invalidate();
396                     }
397                     catch (Throwable e)
398                     {
399                         e.printStackTrace();
400                     }
401                 }
402             });
403         }
404         catch (Throwable e)
405         {
406             e.printStackTrace();
407         }
408     }
409 */
410
411     private Bitmap getBitmap(ReceivedDataHolder receivedData)
412     {
413         try
414         {
415             final byte[] data = receivedData.getData();
416             final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
417             if (imageData == null)
418             {
419                 Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
420                 return (null);
421             }
422 /*
423             else
424             {
425                 Log.v(TAG, "getBitmap() : " + imageData.getByteCount() + "bytes. (offset : " + offsetSize + ")");
426             }
427 */
428             return (imageData);
429         }
430         catch (Exception e)
431         {
432             e.printStackTrace();
433         }
434         return (null);
435     }
436
437     @Override
438     public boolean onTouch(View v, MotionEvent event)
439     {
440         try
441         {
442             int id = v.getId();
443             Log.v(TAG, "onTouch() : " + id);
444             if (event.getAction() == MotionEvent.ACTION_DOWN)
445             {
446                 driveAutoFocus(getPointWithEvent(event));
447                 return (true);
448             }
449         }
450         catch (Exception e)
451         {
452             e.printStackTrace();
453         }
454         return (false);
455     }
456
457     private PointF getPointWithEvent(MotionEvent event)
458     {
459         if (event == null)
460         {
461             return (null);
462         }
463         try
464         {
465             ImageView imageView = activity.findViewById(R.id.imageView);
466             return (new PointF(((event.getX() / (float) imageView.getWidth()) * 100.0f), ((event.getY() / (float) imageView.getHeight()) * 100.0f)));
467         }
468         catch (Exception e)
469         {
470             e.printStackTrace();
471         }
472         return (null);
473     }
474 }