OSDN Git Service

PowerShotZoomで動作するようにしてみる。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / canon / wrapper / liveview / CanonLiveViewControl.java
index 8f2c441..d849167 100644 (file)
@@ -24,18 +24,21 @@ public class CanonLiveViewControl implements ILiveViewControl, ILiveViewListener
     private final String TAG = this.toString();
     private final IPtpIpCommandPublisher commandIssuer;
     private final int delayMs;
-    //private CanonLiveViewImageReceiver imageReceiver;
+    private final boolean isDumpLog = false;
+    private final boolean isSearchJpegHeader;
+    private final int retryCount = 1500;
     private final CanonLiveViewImageReceiver imageReceiver;
     private IImageDataReceiver dataReceiver = null;
     private boolean liveViewIsReceiving = false;
     private boolean commandIssued = false;
 
-    public CanonLiveViewControl(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs)
+    public CanonLiveViewControl(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs, boolean isSearchJpegHeader)
     {
         this.commandIssuer = interfaceProvider.getCommandPublisher();
+        this.isSearchJpegHeader = isSearchJpegHeader;
         this.delayMs = delayMs;
         //this.imageReceiver = new CanonLiveViewImageReceiver(this);
-        this.imageReceiver = new CanonLiveViewImageReceiver(this);
+        this.imageReceiver = new CanonLiveViewImageReceiver(context,this);
         Log.v(TAG, " -=-=-=-=-=- CanonLiveViewControl : delay " + delayMs + " ms");
     }
 
@@ -67,8 +70,12 @@ public class CanonLiveViewControl implements ILiveViewControl, ILiveViewListener
                         {
                             if (!commandIssued)
                             {
+                                if (isDumpLog)
+                                {
+                                    Log.v(TAG, " enqueueCommand() ");
+                                }
                                 commandIssued = true;
-                                commandIssuer.enqueueCommand(new PtpIpCommandGenericWithRetry(imageReceiver, SEQ_GET_VIEWFRAME, delayMs, 2000, false, false, 0, 0x9153, 12, 0x00200000, 0x01, 0x00, 0x00));
+                                commandIssuer.enqueueCommand(new PtpIpCommandGenericWithRetry(imageReceiver, SEQ_GET_VIEWFRAME, delayMs, retryCount, false, false, 0, 0x9153, 12, 0x00200000, 0x01, 0x00, 0x00));
                             }
                             try
                             {
@@ -155,11 +162,14 @@ public class CanonLiveViewControl implements ILiveViewControl, ILiveViewListener
         {
             if ((dataReceiver != null)&&(data != null))
             {
-                Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
-                //dataReceiver.setImageData(data, metadata);
-                if (data.length > 8)
+                if (isDumpLog)
                 {
-                    dataReceiver.setImageData(Arrays.copyOfRange(data, 8, data.length), metadata);  // ヘッダ部分を切り取って送る
+                    Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
+                }
+                int headerSize = searchJpegHeader(data);
+                if (headerSize >= 0)
+                {
+                    dataReceiver.setImageData(Arrays.copyOfRange(data, headerSize, data.length), metadata);  // ヘッダ部分を切り取って送る
                 }
             }
         }
@@ -170,6 +180,39 @@ public class CanonLiveViewControl implements ILiveViewControl, ILiveViewListener
         commandIssued = false;
     }
 
+    private int searchJpegHeader(byte[] data)
+    {
+        if (data.length <= 8)
+        {
+            return (-1);
+        }
+        if (!isSearchJpegHeader)
+        {
+            // JPEG ヘッダを探さない場合は、8バイト固定とする
+            return (8);
+        }
+        try
+        {
+            int size = data.length - 1;
+            int index = 0;
+            while (index < size)
+            {
+                if ((data[index] == (byte) 0xff)&&(data[index + 1] == (byte) 0xd8))
+                {
+                    return (index);
+                }
+                index++;
+            }
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+
+        // 見つからなかったときは 8 を返す
+        return (8);
+    }
+
     @Override
     public void onErrorOccurred(Exception e)
     {