OSDN Git Service

ライブビューデータを拾うために検討中。その4。
[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 import java.io.FileWriter;
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 FileWriter fileWriter = null;
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     private void showMessageText(final String message)
77     {
78         activity.runOnUiThread(new Runnable() {
79             @Override
80             public void run() {
81                 try {
82                     if (textview == null) {
83                         textview = activity.findViewById(R.id.show_information);
84                     }
85                     if (textview != null) {
86                         textview.setText(message);
87                     }
88                 } catch (Exception e) {
89                     e.printStackTrace();
90                 }
91             }
92         });
93     }
94
95     @Override
96     public void onClick(View v)
97     {
98         Log.v(TAG, "onClick : " + v.getId());
99         int id = v.getId();
100         switch (id)
101         {
102             case R.id.button1:
103                 doShutter();
104                 break;
105             case R.id.button2:
106                 readImageFile("sampledata1.bin");
107                 showMessageText("show 'sampledata1.bin'.");
108                 break;
109             case R.id.button3:
110                 readImageFile("sampledata2.bin");
111                 showMessageText("show 'sampledata2.bin'.");
112                 break;
113             case R.id.button4:
114                 readImageFile("sampledata3.bin");
115                 showMessageText("show 'sampledata3.bin'.");
116                 break;
117             default:
118                 showMessageText("Unknown : " + id);
119                 break;
120         }
121     }
122
123     private void doShutter()
124     {
125         Log.v(TAG, "execute shutter");
126         try
127         {
128             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.shutter, Snackbar.LENGTH_SHORT).show();
129             showMessageText("SHUTTER");
130             Thread thread = new Thread(new Runnable() {
131                 @Override
132                 public void run() {
133                     boolean ret = connection.execute_shutter();
134                     if (!ret)
135                     {
136                         showMessageText("SHUTTER FAILURE...");
137                     }
138                 }
139             });
140             thread.start();
141         }
142         catch (Exception e)
143         {
144             e.printStackTrace();
145         }
146     }
147
148     @Override
149     public void updateImage(ReceivedDataHolder receivedData)
150     {
151         try
152         {
153             final byte[] dataValue = receivedData.getData();
154             byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
155             byte[] endJpegMarker   = {(byte)0xff, (byte)0xd9};
156
157             Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
158
159             // ダミーの記録ファイルが開いていたらファイルに書いておく。
160             outputFile(receivedData);
161
162             ///////  Bitmap画像を作る... //////
163             final Bitmap imageData = BitmapFactory.decodeByteArray(dataValue, 18, (dataValue.length - 18));
164             if (imageData == null)
165             {
166                 Log.v(TAG, "imageData is null...");
167             }
168             else
169             {
170                 Log.v(TAG, "imageData : " + imageData.getByteCount() + " bytes.");
171             }
172
173             //////  画像を更新する
174             activity.runOnUiThread(new Runnable() {
175                 @Override
176                 public void run() {
177                     try
178                     {
179                         // ビットマップイメージを表示する。
180                         ImageView view = activity.findViewById(R.id.imageView);
181                         view.setImageBitmap(imageData);
182                         view.invalidate();
183                     }
184                     catch (Throwable e)
185                     {
186                         e.printStackTrace();
187                     }
188                 }
189             });
190         }
191         catch (Throwable e)
192         {
193             e.printStackTrace();
194         }
195     }
196
197     @Override
198     public void updateImage(final Bitmap bitmap)
199     {
200         try
201         {
202             Log.v(TAG, "bitmap : " + bitmap.getByteCount() + " bytes.");
203
204             //////  画像を更新する
205             activity.runOnUiThread(new Runnable() {
206                 @Override
207                 public void run() {
208                     try
209                     {
210                         // ビットマップイメージを表示する。
211                         ImageView view = activity.findViewById(R.id.imageView);
212                         view.setImageBitmap(bitmap);
213                         view.invalidate();
214                     }
215                     catch (Throwable e)
216                     {
217                         e.printStackTrace();
218                     }
219                 }
220             });
221         }
222         catch (Throwable e)
223         {
224             e.printStackTrace();
225         }
226     }
227
228     private void outputFile(ReceivedDataHolder receivedData)
229     {
230         try
231         {
232             if (outputStream != null)
233             {
234                 final byte[] byteData = receivedData.getData();
235                 outputStream.write(byteData, 0, byteData.length);
236             }
237             if (fileWriter != null)
238             {
239                 final char[] charData = receivedData.getCharData();
240                 fileWriter.write(charData, 0, charData.length);
241                 fileWriter.flush();
242             }
243         }
244         catch (Exception e)
245         {
246             e.printStackTrace();
247         }
248     }
249
250     private void prepareFile()
251     {
252         boolean useStream = true;
253         try
254         {
255             final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/AirA01a/";
256             final String outputFileName = "camtest.bin";
257             File filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase());
258             if (useStream) {
259                 outputStream = new FileOutputStream(filepath);
260                 fileWriter = null;
261             } else {
262                 outputStream = null;
263                 fileWriter = new FileWriter(filepath);
264             }
265         }
266         catch (Exception e)
267         {
268             e.printStackTrace();
269             outputStream = null;
270             fileWriter = null;
271         }
272     }
273
274     private void readImageFile(final String readFileName)
275     {
276         Thread thread = new Thread(new Runnable() {
277             @Override
278             public void run() {
279                 readImageFileImpl(readFileName);
280             }
281         });
282         try
283         {
284             thread.start();
285         }
286         catch (Exception e)
287         {
288             e.printStackTrace();
289         }
290     }
291
292     private void readImageFileImpl(final String readFileName)
293     {
294         try
295         {
296             Log.v(TAG, "readImageFileImpl() : " + readFileName);
297             final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/AirA01a/";
298             File filepath = new File(directoryPath.toLowerCase(), readFileName.toLowerCase());
299             FileInputStream istr = new FileInputStream(filepath);
300             final Bitmap imageData = BitmapFactory.decodeStream(istr);
301             istr.close();
302             if (imageData == null)
303             {
304                 Log.v(TAG, "readImageFileImpl() : bitmap is NULL.");
305             }
306             else
307             {
308                 Log.v(TAG, "readImageFileImpl() : bitmap is " + imageData.getByteCount() + " bytes.");
309             }
310
311             //////  画像表示を更新する //////
312             activity.runOnUiThread(new Runnable() {
313                 @Override
314                 public void run() {
315                     try
316                     {
317                         // ビットマップイメージを表示する。
318                         ImageView view = activity.findViewById(R.id.information_view);
319                         view.setImageBitmap(imageData);
320                         view.invalidate();
321                     }
322                     catch (Throwable e)
323                     {
324                         e.printStackTrace();
325                     }
326                 }
327             });
328         }
329         catch (Throwable e)
330         {
331             e.printStackTrace();
332         }
333     }
334 }