OSDN Git Service

ライブビューデータを拾うために検討中。その2。
authorMRSa <mrsa@myad.jp>
Thu, 2 May 2019 13:56:46 +0000 (22:56 +0900)
committerMRSa <mrsa@myad.jp>
Thu, 2 May 2019 13:56:46 +0000 (22:56 +0900)
app/src/main/java/net/osdn/gokigen/cameratest/camtest/CamTest.java
app/src/main/java/net/osdn/gokigen/cameratest/fuji/ReceivedDataHolder.java

index 0ed948e..c088a23 100644 (file)
@@ -20,6 +20,7 @@ import androidx.annotation.NonNull;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 
 public class CamTest implements View.OnClickListener, ILiveViewImage
 {
@@ -28,6 +29,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
     private TextView textview;
     private Connection connection;
     private FileOutputStream outputStream = null;
+    private FileWriter fileWriter = null;
 
     public CamTest(@NonNull Activity activity)
     {
@@ -40,7 +42,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
         Log.v(TAG, "connect request");
         try
         {
-            //prepareFile();
+            prepareFile(false);
 
             Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.connect, Snackbar.LENGTH_SHORT).show();
 
@@ -148,10 +150,7 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
             Log.v(TAG, "Image : "+ dataValue.length + " bytes.");
 
             // ダミーの記録ファイルが開いていたらファイルに書いておく。
-            if (outputStream != null)
-            {
-                outputStream.write(dataValue, 0, dataValue.length);
-            }
+            outputFile(receivedData);
 
             ///////  画像を作る
             final Bitmap imageData = BitmapFactory.decodeByteArray(dataValue, 18, (dataValue.length - 18));
@@ -188,19 +187,47 @@ public class CamTest implements View.OnClickListener, ILiveViewImage
         }
     }
 
-    private void prepareFile()
+    private void outputFile(ReceivedDataHolder receivedData)
+    {
+        try
+        {
+            if (outputStream != null)
+            {
+                final byte[] byteData = receivedData.getData();
+                outputStream.write(byteData, 0, byteData.length);
+            }
+            if (fileWriter != null)
+            {
+                final char[] charData = receivedData.getCharData();
+                fileWriter.write(charData, 0, charData.length);
+            }
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    private void prepareFile(boolean isStream)
     {
         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);
+            if (isStream) {
+                outputStream = new FileOutputStream(filepath);
+                fileWriter = null;
+            } else {
+                outputStream = null;
+                fileWriter = new FileWriter(filepath);
+            }
         }
         catch (Exception e)
         {
             e.printStackTrace();
             outputStream = null;
+            fileWriter = null;
         }
     }
 
index 566874e..ab62d4b 100644 (file)
@@ -12,18 +12,26 @@ import java.util.Arrays;
 public class ReceivedDataHolder
 {
     private final byte[] data;
+    private final char[] charData;
 
     ReceivedDataHolder(byte[] data, int length)
     {
         this.data = Arrays.copyOfRange(data, 0, length);
+        this.charData = null; // Arrays.copyOf(data, length);
     }
 
     ReceivedDataHolder(char[] data, int length)
     {
+        this.charData = data;
         byte[] convertedData = toBytes(data);
         this.data = Arrays.copyOfRange(convertedData, 0, length);
     }
 
+    public char[] getCharData()
+    {
+        return (charData);
+    }
+
     public byte[] getData()
     {
         return (data);