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();
214 queryCameraCapability();
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();
355 private void queryCameraCapability()
359 Thread thread = new Thread(new Runnable() {
362 boolean ret = connection.execute_query_camera_capability();
365 showMessageText("Query Camera Capability...");
370 Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.query_capability, Snackbar.LENGTH_SHORT).show();
380 public void updateImage(ReceivedDataHolder receivedData)
384 //final byte[] dataValue = receivedData.getData();
385 //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
386 //byte[] endJpegMarker = {(byte)0xff, (byte)0xd9};
388 //Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
390 // ダミーの記録ファイルが開いていたらファイルに書いておく。
391 //outputFile(receivedData);
393 /////// Bitmap画像を作る... //////
394 final Bitmap imageData = getBitmap(receivedData);
395 if (imageData != null)
397 // int width = imageData.getWidth();
398 // int height = imageData.getHeight();
399 //if ((width > 300)&&(height > 300))
401 //Log.v(TAG, "bitmap (" + width + "," + height + ")");
404 activity.runOnUiThread(new Runnable() {
409 ImageView view = activity.findViewById(R.id.imageView);
410 view.setImageBitmap(imageData);
412 } catch (Throwable e) {
426 private Bitmap getBitmap(ReceivedDataHolder receivedData)
430 final byte[] data = receivedData.getData();
431 final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
432 if (imageData == null)
434 //Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
447 public boolean onTouch(View v, MotionEvent event)
452 Log.v(TAG, "onTouch() : " + id);
453 if (event.getAction() == MotionEvent.ACTION_DOWN)
455 driveAutoFocus(getPointWithEvent(event));
466 private PointF getPointWithEvent(MotionEvent event)
474 ImageView imageView = activity.findViewById(R.id.imageView);
475 return (new PointF((((event.getX() / (float) imageView.getWidth()) * maxPointLimitWidth) + widthOffset), (((event.getY() / (float) imageView.getHeight()) * maxPointLimitHeight) + heightOffset)));
485 public void statusUpdated(final IFujiStatus cameraStatus)
489 //Log.v(TAG, "statusUpdated()");
492 final InformationView view = activity.findViewById(R.id.information_view);
497 view.drawInformation(cameraStatus);
500 activity.runOnUiThread(new Runnable() {