OSDN Git Service

フォーカスの設定・解除をできるようにした。(EOS M6)
authorMRSa <mrsa@myad.jp>
Fri, 14 Feb 2020 16:22:49 +0000 (01:22 +0900)
committerMRSa <mrsa@myad.jp>
Fri, 14 Feb 2020 16:22:49 +0000 (01:22 +0900)
app/src/main/java/net/osdn/gokigen/a01d/camera/canon/operation/CanonFocusingControl.java
app/src/main/java/net/osdn/gokigen/a01d/camera/canon/wrapper/liveview/CanonLiveViewControl.java
app/src/main/java/net/osdn/gokigen/a01d/camera/ptpip/wrapper/command/PtpIpCommandPublisher.java
app/src/main/java/net/osdn/gokigen/a01d/liveview/LiveViewFragment.java
app/src/main/java/net/osdn/gokigen/a01d/preference/IPreferencePropertyAccessor.java
app/src/main/java/net/osdn/gokigen/a01d/preference/canon/CanonPreferenceFragment.java
app/src/main/res/values-ja/strings.xml
app/src/main/res/values/strings.xml
app/src/main/res/xml/preferences_canon.xml

index e4dc464..1e57a4f 100644 (file)
 package net.osdn.gokigen.a01d.camera.canon.operation;
 
 import android.app.Activity;
+import android.content.SharedPreferences;
+import android.graphics.PointF;
+import android.graphics.RectF;
+import android.util.Log;
 import android.view.MotionEvent;
 
 import androidx.annotation.NonNull;
+import androidx.preference.PreferenceManager;
 
 import net.osdn.gokigen.a01d.camera.IFocusingControl;
+import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.FocusLock;
+import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.FocusUnlock;
+import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.PtpIpCommandPublisher;
+import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
 import net.osdn.gokigen.a01d.liveview.IAutoFocusFrameDisplay;
 import net.osdn.gokigen.a01d.liveview.IIndicatorControl;
+import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
 
-public class CanonFocusingControl implements IFocusingControl
+import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_GET_VIEWFRAME;
+
+public class CanonFocusingControl implements IFocusingControl, IPtpIpCommandCallback
 {
-    private final Activity context;
+    private final String TAG = this.toString();
+    private static final int FOCUS_LOCK_PRE = 15;
+    private static final int FOCUS_LOCK = 16;
+    private static final int FOCUS_MOVE = 17;
+    private static final int FOCUS_UNLOCK = 18;
+
+
+    //private final Activity context;
     private final PtpIpCommandPublisher commandPublisher;
     private final IAutoFocusFrameDisplay frameDisplayer;
     private final IIndicatorControl indicator;
 
+    private float maxPointLimitWidth;
+    private float maxPointLimitHeight;
+    private RectF preFocusFrameRect = null;
+
     public CanonFocusingControl(@NonNull Activity context, @NonNull PtpIpCommandPublisher commandPublisher, IAutoFocusFrameDisplay frameDisplayer, IIndicatorControl indicator)
     {
-        this.context = context;
+        //this.context = context;
         this.commandPublisher = commandPublisher;
         this.frameDisplayer = frameDisplayer;
         this.indicator = indicator;
+        try
+        {
+            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
+            String focusPoint = preferences.getString(IPreferencePropertyAccessor.CANON_FOCUS_XY, IPreferencePropertyAccessor.CANON_FOCUS_XY_DEFAULT_VALUE);
+            String[] focus = focusPoint.split(",");
+            if (focus.length == 2)
+            {
+                maxPointLimitWidth = Integer.parseInt(focus[0]);
+                maxPointLimitHeight = Integer.parseInt(focus[1]);
+            }
+            else
+            {
+                maxPointLimitWidth = 6000.0f;
+                maxPointLimitHeight = 4000.0f;
+            }
+            Log.v(TAG, "FOCUS RESOLUTION : " + maxPointLimitWidth + "," + maxPointLimitHeight);
+
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            maxPointLimitWidth = 6000.0f;
+            maxPointLimitHeight = 4000.0f;
+        }
     }
 
+    @Override
+    public boolean driveAutoFocus(final MotionEvent motionEvent)
+    {
+        Log.v(TAG, "driveAutoFocus()");
+        if (motionEvent.getAction() != MotionEvent.ACTION_DOWN)
+        {
+            return (false);
+        }
+        Thread thread = new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try
+                {
+                    PointF point = frameDisplayer.getPointWithEvent(motionEvent);
+                    if (point != null)
+                    {
+                        preFocusFrameRect = getPreFocusFrameRect(point);
+                        showFocusFrame(preFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Running, 0.0);
+                        if (frameDisplayer.isContainsPoint(point))
+                        {
+                            lockAutoFocus(point);
+                        }
+                    }
+                }
+                catch (Exception e)
+                {
+                    e.printStackTrace();
+                }
+            }
+        });
+        try
+        {
+            thread.start();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        return (false);
+    }
 
     @Override
-    public boolean driveAutoFocus(MotionEvent motionEvent) {
-        return false;
+    public void unlockAutoFocus()
+    {
+        try
+        {
+            Log.v(TAG, " Unlock AF ");
+            commandPublisher.enqueueCommand(new PtpIpCommandGeneric(this, FOCUS_UNLOCK, false, 0, 0x9160));
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
     }
 
     @Override
-    public void unlockAutoFocus() {
+    public void halfPressShutter(boolean isPressed)
+    {
+        unlockAutoFocus();
+        //lockAutoFocus(new PointF(0.5f, 0.5f));
+    }
+
+    private void lockAutoFocus(PointF point)
+    {
+        try
+        {
+            int x = (0x0000ffff & (Math.round(point.x * maxPointLimitWidth) + 1));
+            int y = (0x0000ffff & (Math.round(point.y * maxPointLimitHeight) + 1));
+            Log.v(TAG, "Lock AF: [" + x + ","+ y + "]");
+            commandPublisher.enqueueCommand(new PtpIpCommandGeneric(this, FOCUS_LOCK_PRE, false, 0, 0x9160));
+            commandPublisher.enqueueCommand(new PtpIpCommandGeneric(this, FOCUS_LOCK, 25, false, 0, 0x915b, 16, 0x03, x, y, 0x01));
+            commandPublisher.enqueueCommand(new PtpIpCommandGeneric(this, FOCUS_MOVE, false, 0, 0x9154));
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     *
+     *
+     */
+    private RectF getPreFocusFrameRect(@NonNull PointF point)
+    {
+        float imageWidth =  frameDisplayer.getContentSizeWidth();
+        float imageHeight =  frameDisplayer.getContentSizeHeight();
 
+        // Display a provisional focus frame at the touched point.
+        float focusWidth = 0.125f;  // 0.125 is rough estimate.
+        float focusHeight = 0.125f;
+        if (imageWidth > imageHeight)
+        {
+            focusHeight *= (imageWidth / imageHeight);
+        }
+        else
+        {
+            focusHeight *= (imageHeight / imageWidth);
+        }
+        return (new RectF(point.x - focusWidth / 2.0f, point.y - focusHeight / 2.0f,
+                point.x + focusWidth / 2.0f, point.y + focusHeight / 2.0f));
     }
 
+    /**
+     *
+     *
+     */
+    private void showFocusFrame(RectF rect, IAutoFocusFrameDisplay.FocusFrameStatus status, double duration)
+    {
+        frameDisplayer.showFocusFrame(rect, status, duration);
+        indicator.onAfLockUpdate(IAutoFocusFrameDisplay.FocusFrameStatus.Focused == status);
+    }
+
+    /**
+     *
+     *
+     */
+    private void hideFocusFrame()
+    {
+        frameDisplayer.hideFocusFrame();
+        indicator.onAfLockUpdate(false);
+    }
+
+
     @Override
-    public void halfPressShutter(boolean isPressed) {
+    public void receivedMessage(int id, byte[] rx_body)
+    {
+        try
+        {
+            if ((id == FOCUS_LOCK)||(id == FOCUS_LOCK_PRE))
+            {
+                Log.v(TAG, "FOCUS LOCKED");
+                if (preFocusFrameRect != null)
+                {
+                    // showFocusFrame(preFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Focused, 1.0);  // 1秒だけ表示
+                    showFocusFrame(preFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Focused, -1);
+                }
+            }
+            else if (id == FOCUS_MOVE)
+            {
+                Log.v(TAG, "FOCUS MOVED");
+                if (preFocusFrameRect != null)
+                {
+                    hideFocusFrame();
+                }
+            }
+            else // if (id == FOCUS_UNLOCK)
+            {
+                Log.v(TAG, "FOCUS UNLOCKED");
+                hideFocusFrame();
+            }
+            preFocusFrameRect = null;
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
 
+    @Override
+    public void onReceiveProgress(int currentBytes, int totalBytes, byte[] rx_body)
+    {
+        Log.v(TAG, " " + currentBytes + "/" + totalBytes);
+    }
+
+    @Override
+    public boolean isReceiveMulti()
+    {
+        return (false);
     }
 }
index 8850029..b7a4356 100644 (file)
@@ -64,7 +64,7 @@ public class CanonLiveViewControl implements ILiveViewControl, ILiveViewListener
                             if (!commandIssued)
                             {
                                 commandIssued = true;
-                                commandIssuer.enqueueCommand(new PtpIpCommandGeneric(imageReceiver, SEQ_GET_VIEWFRAME, 10, false, 0, 0x9153, 12, 0x00200000, 0x01, 0x00, 0x00));
+                                commandIssuer.enqueueCommand(new PtpIpCommandGeneric(imageReceiver, SEQ_GET_VIEWFRAME, 20, false, 0, 0x9153, 12, 0x00200000, 0x01, 0x00, 0x00));
                             }
                             try
                             {
@@ -151,7 +151,7 @@ public class CanonLiveViewControl implements ILiveViewControl, ILiveViewListener
         {
             if ((dataReceiver != null)&&(data != null))
             {
-                Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
+                //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
                 //dataReceiver.setImageData(data, metadata);
                 if (data.length > 8)
                 {
index 842693a..23f4b9c 100644 (file)
@@ -479,16 +479,16 @@ public class PtpIpCommandPublisher implements IPtpIpCommandPublisher, IPtpIpComm
                 callback.onReceiveProgress(received_length, target_length, Arrays.copyOfRange(byte_array, 0, received_length));
             }
 
-            do
+            //do
             {
                 sleep(delayMs);
                 read_bytes = is.available();
                 if (read_bytes == 0)
                 {
-                    Log.v(TAG, " WAIT is.available() ... [" + received_length + ", " + target_length + "] retry : " + maxRetryCount);
+                    //Log.v(TAG, " WAIT is.available() ... [" + received_length + ", " + target_length + "] retry : " + maxRetryCount);
                     maxRetryCount--;
                 }
-            } while ((read_bytes == 0)&&(maxRetryCount > 0)&&(received_length < target_length)); // ((read_bytes == 0)&&(estimatedSize > 0)&&(received_length < estimatedSize));
+            } // while ((read_bytes == 0)&&(maxRetryCount > 0)&&(received_length < target_length)); // ((read_bytes == 0)&&(estimatedSize > 0)&&(received_length < estimatedSize));
 
             while ((read_bytes >= 0)&&(received_length < target_length))
             {
@@ -509,7 +509,7 @@ public class PtpIpCommandPublisher implements IPtpIpCommandPublisher, IPtpIpComm
                 //byteStream.write(byte_array, 0, read_bytes);
 
                 maxRetryCount = 20;
-                do
+                //do
                 {
                     sleep(delayMs);
                     read_bytes = is.available();
@@ -519,7 +519,7 @@ public class PtpIpCommandPublisher implements IPtpIpCommandPublisher, IPtpIpComm
                         Log.v(TAG, " WAIT is.available() ... [" + received_length + ", " + target_length + "] " + read_bytes + " retry : " + maxRetryCount);
                         maxRetryCount--;
                     }
-                } while ((read_bytes == 0)&&(maxRetryCount > 0)&&(received_length < target_length)); // while ((read_bytes == 0)&&(estimatedSize > 0)&&(received_length < estimatedSize));
+                } // while ((read_bytes == 0)&&(maxRetryCount > 0)&&(received_length < target_length)); // while ((read_bytes == 0)&&(estimatedSize > 0)&&(received_length < estimatedSize));
             }
             //ByteArrayOutputStream outputStream = cutHeader(byteStream);
             //receivedMessage(isDumpReceiveLog, id, outputStream.toByteArray(), callback);
index 597d0c2..a28c48a 100644 (file)
@@ -361,7 +361,7 @@ public class LiveViewFragment extends Fragment implements IStatusViewDrawer, IFo
                     }
                     if (focusIndicator != null)
                     {
-                        focusIndicator.setVisibility(View.INVISIBLE);
+                        focusIndicator.setVisibility(View.VISIBLE);
                     }
                     if (propertyButton != null)
                     {
index d2c4566..134ffd0 100644 (file)
@@ -67,6 +67,10 @@ public interface IPreferencePropertyAccessor
 
     String USE_OSC_THETA_V21 = "use_osc_theta_v21";
 
+    String CANON_FOCUS_XY = "canon_focus_xy";
+    String CANON_FOCUS_XY_DEFAULT_VALUE = "6000,4000";
+
+
     /*
     int CHOICE_SPLASH_SCREEN = 10;
 
index b75fb92..f2e593f 100644 (file)
@@ -124,6 +124,9 @@ public class CanonPreferenceFragment extends PreferenceFragmentCompat implements
             if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
                 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_FOCUS_XY)) {
+                editor.putString(IPreferencePropertyAccessor.CANON_FOCUS_XY, IPreferencePropertyAccessor.CANON_FOCUS_XY_DEFAULT_VALUE);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
                 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
             }
@@ -257,8 +260,7 @@ public class CanonPreferenceFragment extends PreferenceFragmentCompat implements
     {
         try
         {
-            ListPreference pref;
-            pref = (ListPreference) findPreference(pref_key);
+            ListPreference pref = findPreference(pref_key);
             String value = preferences.getString(key, defaultValue);
             if (pref != null)
             {
index a905832..263d029 100644 (file)
@@ -37,6 +37,8 @@
     <string name="pref_fujix_display_camera_view_summary">お試し:ライブビューをカメラ画面で同時表示します</string>
     <string name="pref_fujix_focus_xy">フォーカス点数 (default: 7,7)</string>
     <string name="pref_summary_fujix_focus_xy">フォーカス点数を指定します</string>
+    <string name="pref_canon_focus_xy">フォーカス点数 (default: 6000,4000)</string>
+    <string name="pref_summary_canon_focus_xy">フォーカス点数を指定します</string>
     <string name="pref_fujix_liveview_wait">LV画像受信待ち間隔(default: 80)</string>
     <string name="pref_summary_fujix_liveview_wait">ライブビュー画像受信間隔を指定します</string>
     <string name="pref_fujix_command_polling_wait">コマンド間隔(default: 500)</string>
index a12f019..bb1e95d 100644 (file)
@@ -38,6 +38,8 @@
     <string name="pref_fujix_display_camera_view_summary">EXPERIMENTAL: Shows liveview screen both camera and smartphone.</string>
     <string name="pref_fujix_focus_xy">Number of Focus Point X,Y (default: 7,7)</string>
     <string name="pref_summary_fujix_focus_xy"> </string>
+    <string name="pref_canon_focus_xy">Number of Focus Point X,Y (default: 6000,4000)</string>
+    <string name="pref_summary_canon_focus_xy"> </string>
     <string name="pref_fujix_liveview_wait">Liveview receive wait (default: 80)</string>
     <string name="pref_summary_fujix_liveview_wait"> </string>
     <string name="pref_fujix_command_polling_wait">Status polling wait (default: 500)</string>
index 79e8438..d513538 100644 (file)
     <PreferenceCategory
         android:title="@string/pref_cat_camera">
 
+        <EditTextPreference
+            android:key="canon_focus_xy"
+            android:title="@string/pref_canon_focus_xy"
+            android:defaultValue="6000,4000"
+            android:summary="@string/pref_summary_canon_focus_xy" />
+
         <CheckBoxPreference
             android:key="capture_both_camera_and_live_view"
             android:title="@string/pref_capture_both_camera_and_live_view" />