1 package net.osdn.gokigen.cameratest.camtest;
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;
14 import com.google.android.material.snackbar.Snackbar;
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;
21 import androidx.annotation.NonNull;
24 import java.io.FileInputStream;
27 public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiveViewImage
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: (謎の)ヘッダ
36 public CamTest(@NonNull Activity activity)
38 this.activity = activity;
39 this.connection = new Connection(this);
44 Log.v(TAG, "connect request");
48 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
50 showMessageText("START CONNECT");
51 Thread thread = new Thread(new Runnable() {
54 boolean ret = connection.start_connect();
57 showMessageText("CONNECT FAILURE...");
69 public void disconnect()
73 connection.disconnect();
81 public void resetConnection()
83 Log.v(TAG, "Reset Connection");
85 showMessageText("RESET CONNECTION");
88 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_reset, Snackbar.LENGTH_SHORT).show();
89 Thread thread = new Thread(new Runnable() {
92 connection.reset_to_camera();
104 public void settings()
106 Log.v(TAG, "settings menu");
108 showMessageText("BBBB");
111 public void valueUp()
113 Log.v(TAG, "value UP");
115 showMessageText("OFFSET : " + offsetSize);
118 public void valueDown()
120 Log.v(TAG, "value DOWN");
122 showMessageText("OFFSET : " + offsetSize);
126 private void showMessageText(final String message)
128 activity.runOnUiThread(new Runnable() {
132 if (textview == null) {
133 textview = activity.findViewById(R.id.show_information);
135 if (textview != null) {
136 textview.setText(message);
138 } catch (Exception e) {
146 public void onClick(View v)
148 Log.v(TAG, "onClick : " + v.getId());
159 //readImageFile("sampledata2.bin");
160 //showMessageText("show 'sampledata2.bin'.");
163 //readImageFile("sampledata3.bin");
164 //showMessageText("show 'sampledata3.bin'.");
167 showMessageText("Unknown : " + id);
172 private void doShutter()
174 Log.v(TAG, "execute shutter");
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() {
182 boolean ret = connection.execute_shutter();
185 showMessageText("SHUTTER FAILURE...");
197 private void driveAutoFocus(final PointF point)
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() {
210 boolean ret = connection.execute_focus_point(point);
213 showMessageText("Auto Focus FAILURE...");
224 private void unlockFocus()
228 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.unlock_focus, Snackbar.LENGTH_SHORT).show();
229 Thread thread = new Thread(new Runnable() {
232 boolean ret = connection.execute_unlock_focus();
235 showMessageText("Unlock Focus FAILURE...");
248 public void updateImage(ReceivedDataHolder receivedData)
252 final byte[] dataValue = receivedData.getData();
253 //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
254 //byte[] endJpegMarker = {(byte)0xff, (byte)0xd9};
256 Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
258 // ダミーの記録ファイルが開いていたらファイルに書いておく。
259 //outputFile(receivedData);
261 /////// Bitmap画像を作る... //////
262 final Bitmap imageData = getBitmap(receivedData);
263 if (imageData != null)
266 activity.runOnUiThread(new Runnable() {
271 ImageView view = activity.findViewById(R.id.imageView);
272 view.setImageBitmap(imageData);
274 } catch (Throwable e) {
288 public void updateImage(final Bitmap bitmap)
292 Log.v(TAG, "bitmap : " + bitmap.getByteCount() + " bytes.");
295 activity.runOnUiThread(new Runnable() {
301 ImageView view = activity.findViewById(R.id.imageView);
302 view.setImageBitmap(bitmap);
318 private void outputFile(ReceivedDataHolder receivedData)
322 if (outputStream != null)
324 final byte[] byteData = receivedData.getData();
325 outputStream.write(byteData, 0, byteData.length);
334 private void prepareFile()
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);
350 private void readImageFile(final String readFileName)
352 Thread thread = new Thread(new Runnable() {
355 readImageFileImpl(readFileName);
368 private void readImageFileImpl(final String readFileName)
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);
378 if (imageData == null)
380 Log.v(TAG, "readImageFileImpl() : bitmap is NULL.");
384 Log.v(TAG, "readImageFileImpl() : bitmap is " + imageData.getByteCount() + " bytes.");
386 ////// 画像表示を更新する //////
387 activity.runOnUiThread(new Runnable() {
393 ImageView view = activity.findViewById(R.id.information_view);
394 view.setImageBitmap(imageData);
411 private Bitmap getBitmap(ReceivedDataHolder receivedData)
415 final byte[] data = receivedData.getData();
416 final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
417 if (imageData == null)
419 Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
425 Log.v(TAG, "getBitmap() : " + imageData.getByteCount() + "bytes. (offset : " + offsetSize + ")");
438 public boolean onTouch(View v, MotionEvent event)
443 Log.v(TAG, "onTouch() : " + id);
444 if (event.getAction() == MotionEvent.ACTION_DOWN)
446 driveAutoFocus(getPointWithEvent(event));
457 private PointF getPointWithEvent(MotionEvent event)
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)));