OSDN Git Service

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