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.content.SharedPreferences;
5 import android.graphics.Bitmap;
6 import android.graphics.BitmapFactory;
7 import android.graphics.PointF;
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.preference.FujiPreferenceFragment;
19 import net.osdn.gokigen.cameratest.fuji.ILiveViewImage;
20 import net.osdn.gokigen.cameratest.fuji.ReceivedDataHolder;
21 import net.osdn.gokigen.cameratest.fuji.preference.IPreferencePropertyAccessor;
22 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatus;
23 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusNotify;
24
25 import androidx.annotation.NonNull;
26 import androidx.preference.PreferenceManager;
27
28 public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiveViewImage, IFujiStatusNotify
29 {
30     private String TAG = toString();
31     private final Activity activity;
32     private TextView textview;
33     private Connection connection;
34
35     private FujiPreferenceFragment preferenceFragment = null;
36     //private FileOutputStream outputStream = null;
37     private static final int offsetSize = 18;  // 4byte: データサイズ、14byte: (謎の)ヘッダ
38
39     private float maxPointLimitWidth = 7.0f;
40     private float maxPointLimitHeight = 7.0f;
41     private float widthOffset = 1.0f;
42     private float heightOffset = 1.0f;
43
44
45     public CamTest(@NonNull Activity activity)
46     {
47         this.activity = activity;
48         this.connection = new Connection(activity, this, this);
49     }
50
51     public void connect()
52     {
53         Log.v(TAG, "connect request");
54         try
55         {
56             //prepareFile();
57             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
58
59             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
60             final boolean isBothLiveView = preferences.getBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
61
62             // フォーカス解像度を変えたい
63             String focusPoint = preferences.getString(IPreferencePropertyAccessor.FUJIX_FOCUS_XY, IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
64             try
65             {
66                 String[] focus = focusPoint.split(",");
67                 if (focus.length == 2)
68                 {
69                     maxPointLimitWidth = Integer.parseInt(focus[0]);
70                     maxPointLimitHeight = Integer.parseInt(focus[1]);
71                 }
72                 Log.v(TAG, "FOCUS RESOLUTION : " + maxPointLimitWidth + "," + maxPointLimitHeight);
73             }
74             catch (Exception e)
75             {
76                 e.printStackTrace();
77                 maxPointLimitWidth = 7.0f;
78                 maxPointLimitHeight = 7.0f;
79             }
80
81             showMessageText("START CONNECT");
82             Thread thread = new Thread(new Runnable() {
83                 @Override
84                 public void run() {
85                     boolean ret = connection.start_connect(isBothLiveView);
86                     if (!ret)
87                     {
88                         showMessageText("CONNECT FAILURE... : " + isBothLiveView);
89                     }
90                 }
91             });
92             thread.start();
93         }
94         catch (Exception e)
95         {
96             e.printStackTrace();
97         }
98     }
99
100     public void disconnect()
101     {
102         Log.v(TAG, "Disconnect");
103
104         showMessageText("DISCONNECT");
105         try
106         {
107             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_disconnect, Snackbar.LENGTH_SHORT).show();
108             Thread thread = new Thread(new Runnable() {
109                 @Override
110                 public void run() {
111                     connection.disconnect();
112                 }
113             });
114             thread.start();
115         }
116         catch (Exception e)
117         {
118             e.printStackTrace();
119         }
120     }
121
122     public void resetConnection()
123     {
124         Log.v(TAG, "Reset Connection");
125
126         showMessageText("RESET CONNECTION");
127         try
128         {
129             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_reset, Snackbar.LENGTH_SHORT).show();
130             Thread thread = new Thread(new Runnable() {
131                 @Override
132                 public void run() {
133                     connection.reset_to_camera();
134                 }
135             });
136             thread.start();
137         }
138         catch (Exception e)
139         {
140             e.printStackTrace();
141         }
142     }
143
144 /*
145     public void settings()
146     {
147         Log.v(TAG, "Show settings menu");
148
149         if (preferenceFragment == null)
150         {
151             preferenceFragment = FujiPreferenceFragment.newInstance();
152         }
153         FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
154         transaction.replace(R.id.fragment1, logCatFragment);
155         // backstackに追加
156         transaction.addToBackStack(null);
157         transaction.commit();
158
159
160         showMessageText("BBBB");
161     }
162
163     public void valueUp()
164     {
165         Log.v(TAG, "value UP");
166         offsetSize++;
167         showMessageText("OFFSET : " + offsetSize);
168     }
169
170     public void valueDown()
171     {
172         Log.v(TAG, "value DOWN");
173         offsetSize--;
174         showMessageText("OFFSET : " + offsetSize);
175     }
176 */
177
178     private void showMessageText(final String message)
179     {
180         activity.runOnUiThread(new Runnable() {
181             @Override
182             public void run() {
183                 try {
184                     if (textview == null) {
185                         textview = activity.findViewById(R.id.show_information);
186                     }
187                     if (textview != null) {
188                         textview.setText(message);
189                     }
190                 } catch (Exception e) {
191                     e.printStackTrace();
192                 }
193             }
194         });
195     }
196
197     @Override
198     public void onClick(View v)
199     {
200         Log.v(TAG, "onClick : " + v.getId());
201         int id = v.getId();
202         switch (id)
203         {
204             case R.id.button1:
205                 doShutter();
206                 break;
207             case R.id.button2:
208                 unlockFocus();
209                 break;
210             case R.id.button3:
211                 changeFilmSimulation();
212                 break;
213             case R.id.button4:
214                 queryCameraCapability();
215                 break;
216             default:
217                 showMessageText("Unknown : " + id);
218                 break;
219         }
220     }
221
222     private void doShutter()
223     {
224         Log.v(TAG, "execute shutter");
225         try
226         {
227             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.shutter, Snackbar.LENGTH_SHORT).show();
228             showMessageText("SHUTTER");
229             Thread thread = new Thread(new Runnable() {
230                 @Override
231                 public void run() {
232                     boolean ret = connection.execute_shutter();
233                     if (!ret)
234                     {
235                         showMessageText("SHUTTER FAILURE...");
236                     }
237                 }
238             });
239             thread.start();
240         }
241         catch (Exception e)
242         {
243             e.printStackTrace();
244         }
245     }
246
247     public void setFocusLimitWidth(float max, float offset)
248     {
249         maxPointLimitWidth = max;
250         widthOffset = offset;
251     }
252
253
254     public void setFocusLimitHeight(float max, float offset)
255     {
256         maxPointLimitHeight = max;
257         heightOffset = offset;
258     }
259
260     private void driveAutoFocus(final PointF point)
261     {
262         if (point == null)
263         {
264             return;
265         }
266         try
267         {
268             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.drive_af, Snackbar.LENGTH_SHORT).show();
269             showMessageText("AF : " + point.x + "," + point.y);
270             Thread thread = new Thread(new Runnable() {
271                 @Override
272                 public void run() {
273                     boolean ret = connection.execute_focus_point(point);
274                     if (!ret)
275                     {
276                         showMessageText("Auto Focus FAILURE...");
277                     }
278                 }
279             });
280             thread.start();
281         }
282         catch (Exception e)
283         {
284             e.printStackTrace();
285         }
286     }
287     private void unlockFocus()
288     {
289         try
290         {
291             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.unlock_focus, Snackbar.LENGTH_SHORT).show();
292             Thread thread = new Thread(new Runnable() {
293                 @Override
294                 public void run() {
295                     boolean ret = connection.execute_unlock_focus();
296                     if (!ret)
297                     {
298                         showMessageText("Unlock Focus FAILURE...");
299                     }
300                 }
301             });
302             thread.start();
303         }
304         catch (Exception e)
305         {
306             e.printStackTrace();
307         }
308     }
309
310     private void changeFilmSimulation()
311     {
312         try
313         {
314             Thread thread = new Thread(new Runnable() {
315                 @Override
316                 public void run() {
317                     boolean ret = connection.execute_change_film_simulation();
318                     if (!ret)
319                     {
320                         showMessageText("Change FilmSimulation Failure...");
321                     }
322                 }
323             });
324             thread.start();
325             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.change_film_simulation, Snackbar.LENGTH_SHORT).show();
326         }
327         catch (Exception e)
328         {
329             e.printStackTrace();
330         }
331     }
332     private void changeImageAspect()
333     {
334         try
335         {
336             Thread thread = new Thread(new Runnable() {
337                 @Override
338                 public void run() {
339                     boolean ret = connection.execute_change_image_aspect();
340                     if (!ret)
341                     {
342                         showMessageText("Change Image aspect Failure...");
343                     }
344                 }
345             });
346             thread.start();
347             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.change_image_aspect, Snackbar.LENGTH_SHORT).show();
348         }
349         catch (Exception e)
350         {
351             e.printStackTrace();
352         }
353     }
354
355     private void queryCameraCapability()
356     {
357         try
358         {
359             Thread thread = new Thread(new Runnable() {
360                 @Override
361                 public void run() {
362                     boolean ret = connection.execute_query_camera_capability();
363                     if (!ret)
364                     {
365                         showMessageText("Query Camera Capability...");
366                     }
367                 }
368             });
369             thread.start();
370             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.query_capability, Snackbar.LENGTH_SHORT).show();
371         }
372         catch (Exception e)
373         {
374             e.printStackTrace();
375         }
376     }
377
378
379     @Override
380     public void updateImage(ReceivedDataHolder receivedData)
381     {
382         try
383         {
384             //final byte[] dataValue = receivedData.getData();
385             //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
386             //byte[] endJpegMarker   = {(byte)0xff, (byte)0xd9};
387
388             //Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
389
390             // ダミーの記録ファイルが開いていたらファイルに書いておく。
391             //outputFile(receivedData);
392
393             ///////  Bitmap画像を作る... //////
394             final Bitmap imageData = getBitmap(receivedData);
395             if (imageData != null)
396             {
397                 // int width = imageData.getWidth();
398                 // int height = imageData.getHeight();
399                 //if ((width > 300)&&(height > 300))
400                 {
401                     //Log.v(TAG, "bitmap (" + width + "," + height + ")");
402
403                     //////  表示画像を更新する
404                     activity.runOnUiThread(new Runnable() {
405                         @Override
406                         public void run() {
407                             try {
408                                 // ビットマップイメージを表示する。
409                                 ImageView view = activity.findViewById(R.id.imageView);
410                                 view.setImageBitmap(imageData);
411                                 view.invalidate();
412                             } catch (Throwable e) {
413                                 e.printStackTrace();
414                             }
415                         }
416                     });
417                 }
418             }
419         }
420         catch (Throwable e)
421         {
422             e.printStackTrace();
423         }
424     }
425
426     private Bitmap getBitmap(ReceivedDataHolder receivedData)
427     {
428         try
429         {
430             final byte[] data = receivedData.getData();
431             final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
432             if (imageData == null)
433             {
434                 //Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
435                 return (null);
436             }
437             return (imageData);
438         }
439         catch (Exception e)
440         {
441             e.printStackTrace();
442         }
443         return (null);
444     }
445
446     @Override
447     public boolean onTouch(View v, MotionEvent event)
448     {
449         try
450         {
451             int id = v.getId();
452             Log.v(TAG, "onTouch() : " + id);
453             if (event.getAction() == MotionEvent.ACTION_DOWN)
454             {
455                 driveAutoFocus(getPointWithEvent(event));
456                 return (true);
457             }
458         }
459         catch (Exception e)
460         {
461             e.printStackTrace();
462         }
463         return (false);
464     }
465
466     private PointF getPointWithEvent(MotionEvent event)
467     {
468         if (event == null)
469         {
470             return (null);
471         }
472         try
473         {
474             ImageView imageView = activity.findViewById(R.id.imageView);
475             return (new PointF((((event.getX() / (float) imageView.getWidth()) * maxPointLimitWidth) + widthOffset), (((event.getY() / (float) imageView.getHeight()) * maxPointLimitHeight) + heightOffset)));
476         }
477         catch (Exception e)
478         {
479             e.printStackTrace();
480         }
481         return (null);
482     }
483
484     @Override
485     public void statusUpdated(final IFujiStatus cameraStatus)
486     {
487         try
488         {
489             //Log.v(TAG, "statusUpdated()");
490
491             // 情報エリアの内容を更新する
492             final InformationView view = activity.findViewById(R.id.information_view);
493             if (view == null)
494             {
495                 return;
496             }
497             view.drawInformation(cameraStatus);
498
499             //////  画像を更新する
500             activity.runOnUiThread(new Runnable() {
501                 @Override
502                 public void run() {
503                     try
504                     {
505                         view.invalidate();
506                     }
507                     catch (Throwable e)
508                     {
509                         e.printStackTrace();
510                     }
511                 }
512             });
513         }
514         catch (Throwable e)
515         {
516             e.printStackTrace();
517         }
518
519     }
520 }