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.util.Log;
8 import android.view.MotionEvent;
9 import android.view.View;
10 import android.widget.ImageView;
11 import android.widget.TextView;
13 import com.google.android.material.snackbar.Snackbar;
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;
23 import androidx.annotation.NonNull;
25 public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiveViewImage, IFujiStatusNotify
27 private String TAG = toString();
28 private final Activity activity;
29 private TextView textview;
30 private Connection connection;
32 private FujiPreferenceFragment preferenceFragment = null;
33 //private FileOutputStream outputStream = null;
34 private static final int offsetSize = 18; // 4byte: データサイズ、14byte: (謎の)ヘッダ
36 private float maxPointLimitWidth = 7.0f;
37 private float maxPointLimitHeight = 7.0f;
38 private float widthOffset = 1.0f;
39 private float heightOffset = 1.0f;
42 public CamTest(@NonNull Activity activity)
44 this.activity = activity;
45 this.connection = new Connection(this, this);
50 Log.v(TAG, "connect request");
54 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
56 showMessageText("START CONNECT");
57 Thread thread = new Thread(new Runnable() {
60 boolean ret = connection.start_connect();
63 showMessageText("CONNECT FAILURE...");
75 public void disconnect()
77 Log.v(TAG, "Disconnect");
79 showMessageText("DISCONNECT");
82 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_disconnect, Snackbar.LENGTH_SHORT).show();
83 Thread thread = new Thread(new Runnable() {
86 connection.disconnect();
97 public void resetConnection()
99 Log.v(TAG, "Reset Connection");
101 showMessageText("RESET CONNECTION");
104 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_reset, Snackbar.LENGTH_SHORT).show();
105 Thread thread = new Thread(new Runnable() {
108 connection.reset_to_camera();
120 public void settings()
122 Log.v(TAG, "Show settings menu");
124 if (preferenceFragment == null)
126 preferenceFragment = FujiPreferenceFragment.newInstance();
128 FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
129 transaction.replace(R.id.fragment1, logCatFragment);
131 transaction.addToBackStack(null);
132 transaction.commit();
135 showMessageText("BBBB");
138 public void valueUp()
140 Log.v(TAG, "value UP");
142 showMessageText("OFFSET : " + offsetSize);
145 public void valueDown()
147 Log.v(TAG, "value DOWN");
149 showMessageText("OFFSET : " + offsetSize);
153 private void showMessageText(final String message)
155 activity.runOnUiThread(new Runnable() {
159 if (textview == null) {
160 textview = activity.findViewById(R.id.show_information);
162 if (textview != null) {
163 textview.setText(message);
165 } catch (Exception e) {
173 public void onClick(View v)
175 Log.v(TAG, "onClick : " + v.getId());
186 //readImageFile("sampledata2.bin");
187 //showMessageText("show 'sampledata2.bin'.");
190 //readImageFile("sampledata3.bin");
191 //showMessageText("show 'sampledata3.bin'.");
194 showMessageText("Unknown : " + id);
199 private void doShutter()
201 Log.v(TAG, "execute shutter");
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() {
209 boolean ret = connection.execute_shutter();
212 showMessageText("SHUTTER FAILURE...");
224 public void setFocusLimitWidth(float max, float offset)
226 maxPointLimitWidth = max;
227 widthOffset = offset;
231 public void setFocusLimitHeight(float max, float offset)
233 maxPointLimitHeight = max;
234 heightOffset = offset;
237 private void driveAutoFocus(final PointF point)
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() {
250 boolean ret = connection.execute_focus_point(point);
253 showMessageText("Auto Focus FAILURE...");
264 private void unlockFocus()
268 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.unlock_focus, Snackbar.LENGTH_SHORT).show();
269 Thread thread = new Thread(new Runnable() {
272 boolean ret = connection.execute_unlock_focus();
275 showMessageText("Unlock Focus FAILURE...");
288 public void updateImage(ReceivedDataHolder receivedData)
292 //final byte[] dataValue = receivedData.getData();
293 //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
294 //byte[] endJpegMarker = {(byte)0xff, (byte)0xd9};
296 //Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
298 // ダミーの記録ファイルが開いていたらファイルに書いておく。
299 //outputFile(receivedData);
301 /////// Bitmap画像を作る... //////
302 final Bitmap imageData = getBitmap(receivedData);
303 if (imageData != null)
305 // int width = imageData.getWidth();
306 // int height = imageData.getHeight();
307 //if ((width > 300)&&(height > 300))
309 //Log.v(TAG, "bitmap (" + width + "," + height + ")");
312 activity.runOnUiThread(new Runnable() {
317 ImageView view = activity.findViewById(R.id.imageView);
318 view.setImageBitmap(imageData);
320 } catch (Throwable e) {
334 private Bitmap getBitmap(ReceivedDataHolder receivedData)
338 final byte[] data = receivedData.getData();
339 final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
340 if (imageData == null)
342 //Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
355 public boolean onTouch(View v, MotionEvent event)
360 Log.v(TAG, "onTouch() : " + id);
361 if (event.getAction() == MotionEvent.ACTION_DOWN)
363 driveAutoFocus(getPointWithEvent(event));
374 private PointF getPointWithEvent(MotionEvent event)
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)));
393 public void statusUpdated(final IFujiStatus cameraStatus)
397 //Log.v(TAG, "statusUpdated()");
400 final InformationView view = activity.findViewById(R.id.information_view);
405 view.drawInformation(cameraStatus);
408 activity.runOnUiThread(new Runnable() {