1 package net.osdn.gokigen.cameratest.camtest;
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;
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.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;
25 import androidx.annotation.NonNull;
26 import androidx.preference.PreferenceManager;
28 public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiveViewImage, IFujiStatusNotify
30 private String TAG = toString();
31 private final Activity activity;
32 private TextView textview;
33 private Connection connection;
35 private FujiPreferenceFragment preferenceFragment = null;
36 //private FileOutputStream outputStream = null;
37 private static final int offsetSize = 18; // 4byte: データサイズ、14byte: (謎の)ヘッダ
39 private float maxPointLimitWidth = 7.0f;
40 private float maxPointLimitHeight = 7.0f;
41 private float widthOffset = 1.0f;
42 private float heightOffset = 1.0f;
45 public CamTest(@NonNull Activity activity)
47 this.activity = activity;
48 this.connection = new Connection(activity, this, this);
53 Log.v(TAG, "connect request");
57 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
59 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
60 final boolean isBothLiveView = preferences.getBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
63 String focusPoint = preferences.getString(IPreferencePropertyAccessor.FUJIX_FOCUS_XY, IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
66 String[] focus = focusPoint.split(",");
67 if (focus.length == 2)
69 maxPointLimitWidth = Integer.parseInt(focus[0]);
70 maxPointLimitHeight = Integer.parseInt(focus[1]);
72 Log.v(TAG, "FOCUS RESOLUTION : " + maxPointLimitWidth + "," + maxPointLimitHeight);
77 maxPointLimitWidth = 7.0f;
78 maxPointLimitHeight = 7.0f;
81 showMessageText("START CONNECT");
82 Thread thread = new Thread(new Runnable() {
85 boolean ret = connection.start_connect(isBothLiveView);
88 showMessageText("CONNECT FAILURE... : " + isBothLiveView);
100 public void disconnect()
102 Log.v(TAG, "Disconnect");
104 showMessageText("DISCONNECT");
107 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_disconnect, Snackbar.LENGTH_SHORT).show();
108 Thread thread = new Thread(new Runnable() {
111 connection.disconnect();
122 public void resetConnection()
124 Log.v(TAG, "Reset Connection");
126 showMessageText("RESET CONNECTION");
129 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_reset, Snackbar.LENGTH_SHORT).show();
130 Thread thread = new Thread(new Runnable() {
133 connection.reset_to_camera();
145 public void settings()
147 Log.v(TAG, "Show settings menu");
149 if (preferenceFragment == null)
151 preferenceFragment = FujiPreferenceFragment.newInstance();
153 FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
154 transaction.replace(R.id.fragment1, logCatFragment);
156 transaction.addToBackStack(null);
157 transaction.commit();
160 showMessageText("BBBB");
163 public void valueUp()
165 Log.v(TAG, "value UP");
167 showMessageText("OFFSET : " + offsetSize);
170 public void valueDown()
172 Log.v(TAG, "value DOWN");
174 showMessageText("OFFSET : " + offsetSize);
178 private void showMessageText(final String message)
180 activity.runOnUiThread(new Runnable() {
184 if (textview == null) {
185 textview = activity.findViewById(R.id.show_information);
187 if (textview != null) {
188 textview.setText(message);
190 } catch (Exception e) {
198 public void onClick(View v)
200 Log.v(TAG, "onClick : " + v.getId());
211 changeFilmSimulation();
217 showMessageText("Unknown : " + id);
222 private void doShutter()
224 Log.v(TAG, "execute shutter");
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() {
232 boolean ret = connection.execute_shutter();
235 showMessageText("SHUTTER FAILURE...");
247 public void setFocusLimitWidth(float max, float offset)
249 maxPointLimitWidth = max;
250 widthOffset = offset;
254 public void setFocusLimitHeight(float max, float offset)
256 maxPointLimitHeight = max;
257 heightOffset = offset;
260 private void driveAutoFocus(final PointF point)
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() {
273 boolean ret = connection.execute_focus_point(point);
276 showMessageText("Auto Focus FAILURE...");
287 private void unlockFocus()
291 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.unlock_focus, Snackbar.LENGTH_SHORT).show();
292 Thread thread = new Thread(new Runnable() {
295 boolean ret = connection.execute_unlock_focus();
298 showMessageText("Unlock Focus FAILURE...");
310 private void changeFilmSimulation()
314 Thread thread = new Thread(new Runnable() {
317 boolean ret = connection.execute_change_film_simulation();
320 showMessageText("Change FilmSimulation Failure...");
325 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.change_film_simulation, Snackbar.LENGTH_SHORT).show();
332 private void changeImageAspect()
336 Thread thread = new Thread(new Runnable() {
339 boolean ret = connection.execute_change_image_aspect();
342 showMessageText("Change Image aspect Failure...");
347 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.change_image_aspect, Snackbar.LENGTH_SHORT).show();
357 public void updateImage(ReceivedDataHolder receivedData)
361 //final byte[] dataValue = receivedData.getData();
362 //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
363 //byte[] endJpegMarker = {(byte)0xff, (byte)0xd9};
365 //Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
367 // ダミーの記録ファイルが開いていたらファイルに書いておく。
368 //outputFile(receivedData);
370 /////// Bitmap画像を作る... //////
371 final Bitmap imageData = getBitmap(receivedData);
372 if (imageData != null)
374 // int width = imageData.getWidth();
375 // int height = imageData.getHeight();
376 //if ((width > 300)&&(height > 300))
378 //Log.v(TAG, "bitmap (" + width + "," + height + ")");
381 activity.runOnUiThread(new Runnable() {
386 ImageView view = activity.findViewById(R.id.imageView);
387 view.setImageBitmap(imageData);
389 } catch (Throwable e) {
403 private Bitmap getBitmap(ReceivedDataHolder receivedData)
407 final byte[] data = receivedData.getData();
408 final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
409 if (imageData == null)
411 //Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
424 public boolean onTouch(View v, MotionEvent event)
429 Log.v(TAG, "onTouch() : " + id);
430 if (event.getAction() == MotionEvent.ACTION_DOWN)
432 driveAutoFocus(getPointWithEvent(event));
443 private PointF getPointWithEvent(MotionEvent event)
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)));
462 public void statusUpdated(final IFujiStatus cameraStatus)
466 //Log.v(TAG, "statusUpdated()");
469 final InformationView view = activity.findViewById(R.id.information_view);
474 view.drawInformation(cameraStatus);
477 activity.runOnUiThread(new Runnable() {