OSDN Git Service

ライブビューデータを拾うために検討中。
authorMRSa <mrsa@myad.jp>
Thu, 2 May 2019 13:05:20 +0000 (22:05 +0900)
committerMRSa <mrsa@myad.jp>
Thu, 2 May 2019 13:05:20 +0000 (22:05 +0900)
app/src/main/java/net/osdn/gokigen/cameratest/MainActivity.java
app/src/main/java/net/osdn/gokigen/cameratest/camtest/CamTest.java
app/src/main/java/net/osdn/gokigen/cameratest/fuji/Communication.java
app/src/main/java/net/osdn/gokigen/cameratest/fuji/Connection.java
app/src/main/java/net/osdn/gokigen/cameratest/fuji/FujiStreamReceiver.java
app/src/main/java/net/osdn/gokigen/cameratest/fuji/ILiveViewImage.java [new file with mode: 0644]
app/src/main/java/net/osdn/gokigen/cameratest/fuji/ReceivedDataHolder.java

index 18f2797..0ed948e 100644 (file)
@@ -1,27 +1,38 @@
 package net.osdn.gokigen.cameratest.camtest;
 
 import android.app.Activity;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.Environment;
 import android.util.Log;
 import android.view.View;
+import android.widget.ImageView;
 import android.widget.TextView;
 
 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.ILiveViewImage;
+import net.osdn.gokigen.cameratest.fuji.ReceivedDataHolder;
 
 import androidx.annotation.NonNull;
 
-public class CamTest implements View.OnClickListener
+import java.io.File;
+import java.io.FileOutputStream;
+
+public class CamTest implements View.OnClickListener, ILiveViewImage
 {
     private String TAG = toString();
     private final Activity activity;
     private TextView textview;
     private Connection connection;
+    private FileOutputStream outputStream = null;
+
     public CamTest(@NonNull Activity activity)
     {
         this.activity = activity;
-        this.connection = new Connection();
+        this.connection = new Connection(this);
     }
 
     public void connect()
@@ -29,6 +40,8 @@ public class CamTest implements View.OnClickListener
         Log.v(TAG, "connect request");
         try
         {
+            //prepareFile();
+
             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
 
             showMessageText("START CONNECT");
@@ -98,7 +111,6 @@ public class CamTest implements View.OnClickListener
         }
     }
 
-
     private void doShutter()
     {
         Log.v(TAG, "execute shutter");
@@ -124,4 +136,72 @@ public class CamTest implements View.OnClickListener
         }
     }
 
+    @Override
+    public void updateImage(ReceivedDataHolder receivedData)
+    {
+        try
+        {
+            final byte[] dataValue = receivedData.getData();
+            byte[] startJpegMarker = {(byte)0xff, (byte)0xd8};
+            byte[] endJpegMarker   = {(byte)0xff, (byte)0xd9};
+
+            Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
+
+            // ダミーの記録ファイルが開いていたらファイルに書いておく。
+            if (outputStream != null)
+            {
+                outputStream.write(dataValue, 0, dataValue.length);
+            }
+
+            ///////  画像を作る
+            final Bitmap imageData = BitmapFactory.decodeByteArray(dataValue, 18, (dataValue.length - 18));
+            if (imageData == null)
+            {
+                Log.v(TAG, "imageData is null...");
+            }
+            else
+            {
+                Log.v(TAG, "imageData : " + imageData.getByteCount() + " bytes.");
+            }
+
+            //////  画像を更新する
+            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)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    private void prepareFile()
+    {
+        try
+        {
+            final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/AirA01a/";
+            final String outputFileName = "camtest.bin";
+            File filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase());
+            outputStream = new FileOutputStream(filepath);
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            outputStream = null;
+        }
+    }
+
 }
index c56f5a1..0591dfe 100644 (file)
@@ -2,6 +2,8 @@ package net.osdn.gokigen.cameratest.fuji;
 
 import android.util.Log;
 
+import androidx.annotation.NonNull;
+
 import java.io.BufferedReader;
 import java.io.DataOutputStream;
 import java.io.InputStreamReader;
@@ -24,9 +26,9 @@ class Communication
     private final FujiStreamReceiver stream;
     private final FujiAsyncResponseReceiver response;
 
-    Communication()
+    Communication(@NonNull ILiveViewImage imageViewer)
     {
-        this.stream = new FujiStreamReceiver(CAMERA_IP, STREAM_PORT);
+        this.stream = new FujiStreamReceiver(CAMERA_IP, STREAM_PORT, imageViewer);
         this.response = new FujiAsyncResponseReceiver(CAMERA_IP, ASYNC_RESPONSE_PORT);
     }
 
index ce2c3c3..209a68b 100644 (file)
@@ -2,15 +2,17 @@ package net.osdn.gokigen.cameratest.fuji;
 
 import android.util.Log;
 
+import androidx.annotation.NonNull;
+
 public class Connection
 {
     private final String TAG = toString();
     private final MessageSequence sequence;
     private final Communication comm;
 
-    public Connection()
+    public Connection(@NonNull ILiveViewImage imageViewer)
     {
-        this.comm = new Communication();
+        this.comm = new Communication(imageViewer);
         this.sequence = new MessageSequence();
     }
 
index 053e135..a675ecf 100644 (file)
@@ -1,6 +1,9 @@
 package net.osdn.gokigen.cameratest.fuji;
 
 import android.util.Log;
+
+import androidx.annotation.NonNull;
+
 import java.io.InputStreamReader;
 import java.net.Socket;
 
@@ -9,14 +12,16 @@ class FujiStreamReceiver
     private final String TAG = toString();
     private final String ipAddress;
     private final int portNumber;
+    private final ILiveViewImage imageViewer;
     private static final int BUFFER_SIZE = 1280 * 1024 + 8;
     private static final int WAIT_MS = 50;
     private boolean isStart = false;
 
-    FujiStreamReceiver(String ip, int portNumber)
+    FujiStreamReceiver(String ip, int portNumber, @NonNull ILiveViewImage imageViewer)
     {
         this.ipAddress = ip;
         this.portNumber = portNumber;
+        this.imageViewer = imageViewer;
     }
 
     void start()
@@ -76,8 +81,7 @@ class FujiStreamReceiver
             try
             {
                 int read_bytes = isr.read(char_array, 0, BUFFER_SIZE);
-                Log.v(TAG, "RECEIVE STREAM : " + read_bytes + " bytes.");
-                //return (new ReceivedDataHolder(char_array, read_bytes));
+                imageViewer.updateImage(new ReceivedDataHolder(char_array, read_bytes));
 
                 Thread.sleep(WAIT_MS);
             }
diff --git a/app/src/main/java/net/osdn/gokigen/cameratest/fuji/ILiveViewImage.java b/app/src/main/java/net/osdn/gokigen/cameratest/fuji/ILiveViewImage.java
new file mode 100644 (file)
index 0000000..ab73c35
--- /dev/null
@@ -0,0 +1,7 @@
+package net.osdn.gokigen.cameratest.fuji;
+
+public interface ILiveViewImage
+{
+    void updateImage(ReceivedDataHolder receivedData);
+
+}
index 4321cdc..566874e 100644 (file)
@@ -9,7 +9,7 @@ import java.util.Arrays;
  *   受信したデータを保持するクラス
  *
  */
-class ReceivedDataHolder
+public class ReceivedDataHolder
 {
     private final byte[] data;
 
@@ -24,7 +24,7 @@ class ReceivedDataHolder
         this.data = Arrays.copyOfRange(convertedData, 0, length);
     }
 
-    byte[] getData()
+    public byte[] getData()
     {
         return (data);
     }