OSDN Git Service

すこしコードを整理。
authorMRSa <mrsa@myad.jp>
Mon, 6 May 2019 12:50:30 +0000 (21:50 +0900)
committerMRSa <mrsa@myad.jp>
Mon, 6 May 2019 12:50:30 +0000 (21:50 +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/MessageSequence.java
app/src/main/res/menu/menu_main.xml
app/src/main/res/values/strings.xml

index a9caab9..6425395 100644 (file)
@@ -113,7 +113,6 @@ public class MainActivity extends AppCompatActivity
         initializeClass();
         prepareClass();
         onReadyClass();
-
     }
 
     /**
@@ -175,6 +174,11 @@ public class MainActivity extends AppCompatActivity
             exitApplication();
             return (true);
         }
+        if (id == R.id.action_reset)
+        {
+            testTarget.resetConnection();
+            return (true);
+        }
         if (id == R.id.action_up_value)
         {
             testTarget.valueUp();
@@ -242,6 +246,7 @@ public class MainActivity extends AppCompatActivity
     {
         try
         {
+            testTarget.disconnect();
             finish();
         }
         catch (Exception e)
index e89b583..d4bb456 100644 (file)
@@ -20,7 +20,6 @@ import androidx.annotation.NonNull;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 
 
 public class CamTest implements View.OnClickListener, ILiveViewImage
@@ -29,7 +28,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
     private final Activity activity;
     private TextView textview;
     private Connection connection;
-    private FileOutputStream outputStream = null;
+    //private FileOutputStream outputStream = null;
     private int offsetSize = 18;  // 4byte: データサイズ、14byte: (謎の)ヘッダ
 
     public CamTest(@NonNull Activity activity)
@@ -43,8 +42,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
         Log.v(TAG, "connect request");
         try
         {
-            prepareFile();
-
+            //prepareFile();
             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
 
             showMessageText("START CONNECT");
@@ -66,6 +64,42 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
         }
     }
 
+    public void disconnect()
+    {
+        try
+        {
+            connection.disconnect();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    public void resetConnection()
+    {
+        Log.v(TAG, "Reset Connection");
+
+        showMessageText("RESET CONNECTION");
+        try
+        {
+            Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_reset, Snackbar.LENGTH_SHORT).show();
+            Thread thread = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    connection.reset_to_camera();
+                }
+            });
+            thread.start();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+
+    }
+
+
     public void settings()
     {
         Log.v(TAG, "settings menu");
@@ -172,7 +206,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
             Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
 
             // ダミーの記録ファイルが開いていたらファイルに書いておく。
-            outputFile(receivedData);
+            //outputFile(receivedData);
 
             ///////  Bitmap画像を作る... //////
             final Bitmap imageData = getBitmap(receivedData);
@@ -230,7 +264,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
             e.printStackTrace();
         }
     }
-
+/*
     private void outputFile(ReceivedDataHolder receivedData)
     {
         try
@@ -262,7 +296,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
             outputStream = null;
         }
     }
-
+*/
     private void readImageFile(final String readFileName)
     {
         Thread thread = new Thread(new Runnable() {
@@ -295,11 +329,12 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
             {
                 Log.v(TAG, "readImageFileImpl() : bitmap is NULL.");
             }
+/*
             else
             {
                 Log.v(TAG, "readImageFileImpl() : bitmap is " + imageData.getByteCount() + " bytes.");
             }
-
+*/
             //////  画像表示を更新する //////
             activity.runOnUiThread(new Runnable() {
                 @Override
@@ -333,12 +368,15 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
             final Bitmap imageData = BitmapFactory.decodeByteArray(data, offsetSize, (data.length - offsetSize));
             if (imageData == null)
             {
-                Log.v(TAG, "readImageFileImpl() : bitmap is NULL. (offset : " + offsetSize + ")");
+                Log.v(TAG, "getBitmap() : NULL. (offset : " + offsetSize + ")");
+                return (null);
             }
+/*
             else
             {
-                Log.v(TAG, "readImageFileImpl() : bitmap is " + imageData.getByteCount() + " bytes.");
+                Log.v(TAG, "getBitmap() : " + imageData.getByteCount() + "bytes. (offset : " + offsetSize + ")");
             }
+*/
             return (imageData);
         }
         catch (Exception e)
index bbdde11..851cc0f 100644 (file)
@@ -7,7 +7,6 @@ import androidx.annotation.NonNull;
 import java.io.BufferedReader;
 import java.io.DataOutputStream;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.net.Socket;
 
 class Communication
@@ -111,23 +110,6 @@ class Communication
 
     ReceivedDataHolder receive_from_camera()
     {
-/*
-        InputStreamReader isr;
-        char[] char_array = new char[BUFFER_SIZE];
-        try
-        {
-            //Log.v(TAG, "receive_from_camera() : start.");
-            isr  = new InputStreamReader(socket.getInputStream());
-            int read_bytes = isr.read(char_array, 0, BUFFER_SIZE);
-            Log.v(TAG, "receive_from_camera() : " + read_bytes + " bytes.");
-            return (new ReceivedDataHolder(char_array, read_bytes));
-        }
-        catch (Exception e)
-        {
-            e.printStackTrace();
-        }
-        return (new ReceivedDataHolder(char_array, 0));
-*/
         try
         {
             byte[] byte_array = new byte[BUFFER_SIZE];
@@ -173,6 +155,7 @@ class Communication
     {
         stream.start();
     }
+
     void stop_stream()
     {
         stream.stop();
@@ -182,6 +165,7 @@ class Communication
     {
         response.start();
     }
+
     void stop_response()
     {
         response.stop();
index 209a68b..435bd40 100644 (file)
@@ -107,11 +107,11 @@ public class Connection
             // 応答OKの場合は、8バイト ({0x03, 0x00, 0x01, 0x20} + {0x10, 0x02, 0x00, 0x00} )が応答されるはず
             rx_bytes = comm.receive_from_camera();
             dump_bytes(11, rx_bytes);
-            Thread.sleep(50);
+            Thread.sleep(150);
 
 
             // 別のポートもオープンして動作を行う。 (1500ms程度待つといけるみたいだ...)
-            Thread.sleep(1500);
+            Thread.sleep(2000);
             comm.start_stream();
             comm.start_response();
 
@@ -125,6 +125,35 @@ public class Connection
         return (false);
     }
 
+    public void reset_to_camera()
+    {
+        try
+        {
+            comm.send_to_camera(sequence.reset_message());
+            ReceivedDataHolder rx_bytes = comm.receive_from_camera();
+            dump_bytes(0, rx_bytes);
+            Thread.sleep(150);
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    public void disconnect()
+    {
+        try
+        {
+            comm.stop_stream();
+            comm.stop_response();
+            comm.disconnect_socket();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
     private boolean get_current_settings()
     {
         try
@@ -181,7 +210,6 @@ public class Connection
             // なんで2回受信... でもやってみる
             rx_bytes = comm.receive_from_camera();
             dump_bytes(15, rx_bytes);
-
         }
         catch (Exception e)
         {
index 535426d..daa08e2 100644 (file)
@@ -11,8 +11,8 @@ class FujiStreamReceiver
     private final String ipAddress;
     private final int portNumber;
     private final ILiveViewImage imageViewer;
+    private static final int WAIT_MS = 75;
     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, @NonNull ILiveViewImage imageViewer)
@@ -59,7 +59,6 @@ class FujiStreamReceiver
 
     private void startReceive(Socket socket)
     {
-/**/
         InputStream isr;
         byte[] byteArray;
         try
@@ -73,28 +72,12 @@ class FujiStreamReceiver
             Log.v(TAG, "===== startReceive() aborted.");
             return;
         }
-/**/
-        Log.v(TAG, "startReceive() start.");
         while (isStart)
         {
             try
             {
-/*
-                Bitmap imageData = BitmapFactory.decodeStream(socket.getInputStream());
-                if (imageData != null)
-                {
-                    // received image
-                    Log.v(TAG, "RECEIVED IMAGE.");
-                }
-                else
-                {
-                    Log.v(TAG, "IMAGE IS NULL...");
-                }
-*/
-/**/
                 int read_bytes = isr.read(byteArray, 0, BUFFER_SIZE);
                 imageViewer.updateImage(new ReceivedDataHolder(byteArray, read_bytes));
-/**/
                 Thread.sleep(WAIT_MS);
             }
             catch (Exception e)
@@ -102,7 +85,6 @@ class FujiStreamReceiver
                 e.printStackTrace();
             }
         }
-/**/
         try
         {
             isr.close();
@@ -111,7 +93,5 @@ class FujiStreamReceiver
         {
             e.printStackTrace();
         }
-/**/
-        Log.v(TAG, "startReceive() end.");
     }
 }
index 8341995..9ea8c4d 100644 (file)
@@ -45,13 +45,19 @@ class MessageSequence
                 (byte)0x47, (byte)0x00, (byte)0x45, (byte)0x00, (byte)0x4e, (byte)0x00, (byte)0x5f, (byte)0x00,
                 (byte)0x5f, (byte)0x00, (byte)0x5f, (byte)0x00, (byte)0x5f, (byte)0x00, (byte)0x30, (byte)0x00,
                 (byte)0x31, (byte)0x00, (byte)0x32, (byte)0x00, (byte)0x33, (byte)0x00, (byte)0x34, (byte)0x00,
-                (byte)0x35, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
+                (byte)0x35, (byte)0x00, (byte)0x36, (byte)0x00, (byte)0x37, (byte)0x00, (byte)0x00, (byte)0x00,
                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
 /**/
         });
     }
 
+    byte[] reset_message()
+    {
+        return (new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, });
+    }
+
+
     byte[] hello_message()
     {
         return (new byte[] {
index e9ce3e6..0fc177c 100644 (file)
         android:title="@string/action_wifi_settings"
         app:showAsAction="never" />
     <item
+        android:id="@+id/action_reset"
+        android:orderInCategory="100"
+        android:title="@string/action_reset"
+        app:showAsAction="never" />
+    <item
         android:id="@+id/action_up_value"
         android:orderInCategory="100"
         android:title="@string/action_up"
index 996cfa3..d4dd566 100644 (file)
@@ -14,4 +14,5 @@
     <string name="label_button5">5</string>
     <string name="action_up">Up</string>
     <string name="action_down">Down</string>
+    <string name="action_reset">Reset Connection</string>
 </resources>