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                 changeImageAspect();
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
356     @Override
357     public void updateImage(ReceivedDataHolder receivedData)
358     {
359         try
360         {
361             //final byte[] dataValue = receivedData.getData();
362             //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
363             //byte[] endJpegMarker   = {(byte)0xff, (byte)0xd9};
364
365             //Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
366
367             // ダミーの記録ファイルが開いていたらファイルに書いておく。
368             //outputFile(receivedData);
369
370             ///////  Bitmap画像を作る... //////
371             final Bitmap imageData = getBitmap(receivedData);
372             if (imageData != null)
373             {
374                 // int width = imageData.getWidth();
375                 // int height = imageData.getHeight();
376                 //if ((width > 300)&&(height > 300))
377                 {
378                     //Log.v(TAG, "bitmap (" + width + "," + height + ")");
379
380                     //////  表示画像を更新する
381                     activity.runOnUiThread(new Runnable() {
382                         @Override
383                         public void run() {
384                             try {
385                                 // ビットマップイメージを表示する。
386                                 ImageView view = activity.findViewById(R.id.imageView);
387                                 view.setImageBitmap(imageData);
388                                 view.invalidate();
389                             } catch (Throwable e) {
390                                 e.printStackTrace();
391                             }
392                         }
393                     });
394                 }
395             }
396         }
397         catch (Throwable e)
398         {
399             e.printStackTrace();
400         }
401     }
402
403     private Bitmap getBitmap(ReceivedDataHolder receivedData)
404     {
405         try
406         {
407             final byte[] data = receivedData.getData();
408             final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
409             if (imageData == null)
410             {
411                 //Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
412                 return (null);
413             }
414             return (imageData);
415         }
416         catch (Exception e)
417         {
418             e.printStackTrace();
419         }
420         return (null);
421     }
422
423     @Override
424     public boolean onTouch(View v, MotionEvent event)
425     {
426         try
427         {
428             int id = v.getId();
429             Log.v(TAG, "onTouch() : " + id);
430             if (event.getAction() == MotionEvent.ACTION_DOWN)
431             {
432                 driveAutoFocus(getPointWithEvent(event));
433                 return (true);
434             }
435         }
436         catch (Exception e)
437         {
438             e.printStackTrace();
439         }
440         return (false);
441     }
442
443     private PointF getPointWithEvent(MotionEvent event)
444     {
445         if (event == null)
446         {
447             return (null);
448         }
449         try
450         {
451             ImageView imageView = activity.findViewById(R.id.imageView);
452             return (new PointF((((event.getX() / (float) imageView.getWidth()) * maxPointLimitWidth) + widthOffset), (((event.getY() / (float) imageView.getHeight()) * maxPointLimitHeight) + heightOffset)));
453         }
454         catch (Exception e)
455         {
456             e.printStackTrace();
457         }
458         return (null);
459     }
460
461     @Override
462     public void statusUpdated(final IFujiStatus cameraStatus)
463     {
464         try
465         {
466             //Log.v(TAG, "statusUpdated()");
467
468             // 情報エリアの内容を更新する
469             final InformationView view = activity.findViewById(R.id.information_view);
470             if (view == null)
471             {
472                 return;
473             }
474             view.drawInformation(cameraStatus);
475
476             //////  画像を更新する
477             activity.runOnUiThread(new Runnable() {
478                 @Override
479                 public void run() {
480                     try
481                     {
482                         view.invalidate();
483                     }
484                     catch (Throwable e)
485                     {
486                         e.printStackTrace();
487                     }
488                 }
489             });
490         }
491         catch (Throwable e)
492         {
493             e.printStackTrace();
494         }
495
496     }
497 }