OSDN Git Service

フォーカスのエリアサイズを変更できるようにした。
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / camtest / CamTest.java
index 37a4ad2..6c87ce6 100644 (file)
@@ -1,6 +1,7 @@
 package net.osdn.gokigen.cameratest.camtest;
 
 import android.app.Activity;
+import android.content.SharedPreferences;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.PointF;
@@ -14,12 +15,16 @@ import com.google.android.material.snackbar.Snackbar;
 
 import net.osdn.gokigen.cameratest.R;
 import net.osdn.gokigen.cameratest.fuji.Connection;
+import net.osdn.gokigen.cameratest.fuji.preference.FujiPreferenceFragment;
 import net.osdn.gokigen.cameratest.fuji.ILiveViewImage;
 import net.osdn.gokigen.cameratest.fuji.ReceivedDataHolder;
+import net.osdn.gokigen.cameratest.fuji.preference.IPreferencePropertyAccessor;
 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatus;
 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusNotify;
 
 import androidx.annotation.NonNull;
+import androidx.preference.EditTextPreference;
+import androidx.preference.PreferenceManager;
 
 public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiveViewImage, IFujiStatusNotify
 {
@@ -27,9 +32,17 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
     private final Activity activity;
     private TextView textview;
     private Connection connection;
+
+    private FujiPreferenceFragment preferenceFragment = null;
     //private FileOutputStream outputStream = null;
     private static final int offsetSize = 18;  // 4byte: データサイズ、14byte: (謎の)ヘッダ
 
+    private float maxPointLimitWidth = 7.0f;
+    private float maxPointLimitHeight = 7.0f;
+    private float widthOffset = 1.0f;
+    private float heightOffset = 1.0f;
+
+
     public CamTest(@NonNull Activity activity)
     {
         this.activity = activity;
@@ -44,14 +57,36 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
             //prepareFile();
             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
 
+            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
+            final boolean isBothLiveView = preferences.getBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
+
+            // フォーカス解像度を変えたい
+            String focusPoint = preferences.getString(IPreferencePropertyAccessor.FUJIX_FOCUS_XY, IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
+            try
+            {
+                String[] focus = focusPoint.split(",");
+                if (focus.length == 2)
+                {
+                    maxPointLimitWidth = Integer.parseInt(focus[0]);
+                    maxPointLimitHeight = Integer.parseInt(focus[1]);
+                }
+                Log.v(TAG, "FOCUS RESOLUTION : " + maxPointLimitWidth + "," + maxPointLimitHeight);
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+                maxPointLimitWidth = 7.0f;
+                maxPointLimitHeight = 7.0f;
+            }
+
             showMessageText("START CONNECT");
             Thread thread = new Thread(new Runnable() {
                 @Override
                 public void run() {
-                    boolean ret = connection.start_connect();
+                    boolean ret = connection.start_connect(isBothLiveView);
                     if (!ret)
                     {
-                        showMessageText("CONNECT FAILURE...");
+                        showMessageText("CONNECT FAILURE... : " + isBothLiveView);
                     }
                 }
             });
@@ -65,9 +100,19 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
 
     public void disconnect()
     {
+        Log.v(TAG, "Disconnect");
+
+        showMessageText("DISCONNECT");
         try
         {
-            connection.disconnect();
+            Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_disconnect, Snackbar.LENGTH_SHORT).show();
+            Thread thread = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    connection.disconnect();
+                }
+            });
+            thread.start();
         }
         catch (Exception e)
         {
@@ -95,12 +140,23 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
         {
             e.printStackTrace();
         }
-
     }
+
 /*
     public void settings()
     {
-        Log.v(TAG, "settings menu");
+        Log.v(TAG, "Show settings menu");
+
+        if (preferenceFragment == null)
+        {
+            preferenceFragment = FujiPreferenceFragment.newInstance();
+        }
+        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
+        transaction.replace(R.id.fragment1, logCatFragment);
+        // backstackに追加
+        transaction.addToBackStack(null);
+        transaction.commit();
+
 
         showMessageText("BBBB");
     }
@@ -191,6 +247,19 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
         }
     }
 
+    public void setFocusLimitWidth(float max, float offset)
+    {
+        maxPointLimitWidth = max;
+        widthOffset = offset;
+    }
+
+
+    public void setFocusLimitHeight(float max, float offset)
+    {
+        maxPointLimitHeight = max;
+        heightOffset = offset;
+    }
+
     private void driveAutoFocus(final PointF point)
     {
         if (point == null)
@@ -246,11 +315,11 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
     {
         try
         {
-            final byte[] dataValue = receivedData.getData();
+            //final byte[] dataValue = receivedData.getData();
             //byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
             //byte[] endJpegMarker   = {(byte)0xff, (byte)0xd9};
 
-            Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
+            //Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
 
             // ダミーの記録ファイルが開いていたらファイルに書いておく。
             //outputFile(receivedData);
@@ -259,20 +328,27 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
             final Bitmap imageData = getBitmap(receivedData);
             if (imageData != null)
             {
-                //////  画像を更新する
-                activity.runOnUiThread(new Runnable() {
-                    @Override
-                    public void run() {
-                        try {
-                            // ビットマップイメージを表示する。
-                            ImageView view = activity.findViewById(R.id.imageView);
-                            view.setImageBitmap(imageData);
-                            view.invalidate();
-                        } catch (Throwable e) {
-                            e.printStackTrace();
+                // int width = imageData.getWidth();
+                // int height = imageData.getHeight();
+                //if ((width > 300)&&(height > 300))
+                {
+                    //Log.v(TAG, "bitmap (" + width + "," + height + ")");
+
+                    //////  表示画像を更新する
+                    activity.runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            try {
+                                // ビットマップイメージを表示する。
+                                ImageView view = activity.findViewById(R.id.imageView);
+                                view.setImageBitmap(imageData);
+                                view.invalidate();
+                            } catch (Throwable e) {
+                                e.printStackTrace();
+                            }
                         }
-                    }
-                });
+                    });
+                }
             }
         }
         catch (Throwable e)
@@ -289,7 +365,7 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
             final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
             if (imageData == null)
             {
-                Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
+                //Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
                 return (null);
             }
             return (imageData);
@@ -330,7 +406,7 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
         try
         {
             ImageView imageView = activity.findViewById(R.id.imageView);
-            return (new PointF(((event.getX() / (float) imageView.getWidth()) * 255.0f), ((event.getY() / (float) imageView.getHeight()) * 255.0f)));
+            return (new PointF((((event.getX() / (float) imageView.getWidth()) * maxPointLimitWidth) + widthOffset), (((event.getY() / (float) imageView.getHeight()) * maxPointLimitHeight) + heightOffset)));
         }
         catch (Exception e)
         {
@@ -344,7 +420,7 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
     {
         try
         {
-            Log.v(TAG, "statusUpdated()");
+            //Log.v(TAG, "statusUpdated()");
 
             // 情報エリアの内容を更新する
             final InformationView view = activity.findViewById(R.id.information_view);