2017-10-14 254 views
-1

我正在創建一個應用程序,我在這裏做一些實時圖像分析並將它們存儲到csv文件中。 csv有2列時間和每幀的y值。如何讀取CSV文件?

我想讀取此文件並將2列中的值存儲到雙數組中。我想要這個,因爲我想對數據執行快速傅里葉變換。

public class MainActivity extends AppCompatActivity implements CameraView.PreviewReadyCallback { 
private static Camera camera = null; 
private CameraView image = null; 

private LineChart bp_graph; 
private int img_Y_Avg, img_U_Avg, img_V_Avg; 
private long end = 0, begin = 0; 
double valueY, valueU, valueV; 
Handler handler; 
private int readingRemaining = 1200; 
private static long time1, time2, timeDifference; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    bp_graph = (LineChart)findViewById(R.id.graph); 


    graph_features(); 

    //open camera 
    try { 
     camera = Camera.open(); 

     handler = new Handler(); 
     final Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       camera.stopPreview(); 
       camera.release(); 
      } 
     }; 
     handler.postDelayed(runnable, 30000); 

    } catch (Exception e) { 
     Log.d("ERROR", "Failed to get camera: " + e.getMessage()); 
    } 

    if (camera != null) { 
     image = new CameraView(this, camera); 
     FrameLayout camera_view = (FrameLayout) findViewById(R.id.camera_view); 
     camera_view.addView(image); 
     image.setOnPreviewReady(this); 
    } 


} 


@Override 
protected void onResume(){ 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
} 


@Override 
public void onPreviewFrame(long startTime, int ySum, int uSum, int vSum, long endTime) { 
    begin = startTime; 
    img_Y_Avg = ySum; 
    img_U_Avg = uSum; 
    img_V_Avg = vSum; 
    end = endTime; 

    showResults(begin, img_Y_Avg, img_U_Avg, img_V_Avg, end); 


} 

private void showResults(long startTime, int ySum, int uSum, int vSum, long endTime){ 

    //set value of Y on the text view 
    TextView valueOfY = (TextView)findViewById(R.id.valueY); 
    //valueY = img_Y_Avg; 
    valueOfY.setText(String.valueOf(img_Y_Avg)); 

    //start time in milliseconds 
    long StartDurationInMs = TimeUnit.MILLISECONDS.convert(begin, TimeUnit.MILLISECONDS); 
    ArrayList<Long> startOfTime = new ArrayList<>(); 
    startOfTime.add(StartDurationInMs); 

    //store value to array list 
    ArrayList<Integer> yAverage = new ArrayList<>(); 
    yAverage.add(img_Y_Avg); 

    //convert to readable format 
    String readableDate = new SimpleDateFormat("MMM dd,yyyy, HH:mm:ss.SSS").format(EndDurationInMs); 
    Log.d("Date ", readableDate); 


    Log.d("time ", String.valueOf(String.valueOf(yAverage.size()))); 
    //store when all array are generated 
    Log.d("time ", String.valueOf(StartDurationInMs)); 


    ArrayList<Long> getValues = new ArrayList<>(); 

    for(int i = 0; i < yAverage.size(); i++) { 
     getValues.add(startOfTime.get(i)); 
     getValues.add((long)(yAverage.get(i))); 
    } 

    //store the yAverage and start time to csv file 
    storeCsv(yAverage, getValues); 


    Log.d("MyEntryData", String.valueOf(getValues)); 

} 

public void storeCsv(ArrayList<Integer>yAverage, ArrayList<Long>getValues){ 

    String filename = "temporary.csv"; 

    //File directoryDownload = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bpReader"; 
    //File logDir = new File (directoryDownload, "bpReader"); //Creates a new folder in DOWNLOAD directory 
    File logDir = new File(path); 
    logDir.mkdirs(); 
    File file = new File(logDir, filename); 


    FileOutputStream outputStream = null; 
     try { 
      file.createNewFile(); 
      outputStream = new FileOutputStream(file, true); 
      //outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 
      for (int i = 0; i < yAverage.size(); i += 2) { 
       outputStream.write((getValues.get(i) + ",").getBytes()); 
       outputStream.write((getValues.get(i + 1) + "\n").getBytes()); 
       //outputStream.write((getValues.get(i + 2) + ",").getBytes()); 
       //outputStream.write((getValues.get(i + 3) + "\n").getBytes()); 
      } 
      outputStream.flush(); 
      outputStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 

public void readCsv(){ 

} 
} 

這是我MainActivity。我在這裏做的是通過我創建的界面的幫助從每個幀獲取CameraView類的數據。之後im將這些值存儲到名爲temporary.csv的CSV文件中。

問題

  1. 我想讀此CSV和第一列(的時間)存儲到一個雙陣列和第二柱(yAverage)到另一個雙陣列。
  2. 我也想刪除文件一旦我把所有的數據存儲到雙數組中。

我該怎麼做?

+0

你在哪裏csv文件存儲在資產或服務器 –

+0

它存儲到我的外部存儲我的設備。如果您看到'storeCsv'方法,您可以看到我將它存儲到外部存儲器中,該存儲器位於名爲'bpReader'的文件夾中。 – Mill3r

+0

這裏是一個很好的例子[如何在Android中讀取csv文件](https://inducesmile.com/android-tips/android-how-to-read-csv-file-from-remote-server-or-assets -folder-in-android /)它可以幫助你看看 –

回答

1

我建議你使用OpenCSV這樣的開源庫來從CSV文件中獲取數據。當你實現了庫時,只需要遍歷x和y列並將它們分配給一個數組。使用OpenCSV它看起來就是這樣。但是,如果具有相同指數座標的x和y彼此相關,我還會建議您採用更多的對象orientec方法。

String csvFile = "/Users/mkyong/csv/country3.csv"; 
    int length = 100; //If you dont know how many entries the csv file has i would suggest to use ArrayList 
    double[] xCoords = new double[length]; 
    double[] yCoords = new double[length]; 


    CSVReader reader = null; 
    try { 
     reader = new CSVReader(new FileReader(csvFile)); 
     String[] line; 
     int i = 0; 
     while ((line = reader.readNext()) != null) { 
      xCoords[i] = Double.parseDouble(line[0]); 
      yCoords[i] = Double.parseDouble(line[1]); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

我記錄了我存儲的csv文件的路徑,發現它是'/ storage/emulated/0/bpReader/temporary.csv',所以對於'csvFile'我指定了這個路徑。我在'reader = new CSVReader(new FileReader(csvFile))';'''java.lang.NoClassDefFoundError:Failed resolution of:Lorg/apache/commons/lang3/ObjectUtils;''行中得到錯誤。我仍然對如何指定我的文件的路徑感到困惑。 – Mill3r

+0

解決了這個問題..必須添加'https:// mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.6'到gradle。 並將文件路徑設置爲 'String getPath = Environment。getExternalStorageDirectory()+「/ bpReader」; String csvFile =「temporary.csv」; String path = getPath +「/」+ csvFile;' – Mill3r

0

由盧卡斯給出了答案,我的方向我的解決方案

public void readCsv(){ 
    //set the path to the file 
    String getPath = Environment.getExternalStorageDirectory() + "/bpReader"; 
    String csvFile = "temporary.csv"; 
    String path = getPath+ "/" + csvFile; 


    //File file = new File(path, csvFile); 
    int length = 500; 
    double[] xCoords = new double[length]; 
    double[] yCoords = new double[length]; 


    CSVReader reader = null; 
    try { 
     File myFile = new File (path); 
     reader = new CSVReader(new FileReader(myFile)); 
     String[] line; 
     int i = 0; 
     while ((line = reader.readNext()) != null) { 
      xCoords[i] = Double.parseDouble(line[0]) ; 
      yCoords[i] = Double.parseDouble(line[1]); 
      Log.d("read:: ", "Time: "+String.valueOf(xCoords[i])+" Y: "+String.valueOf(yCoords[i])); 
     } 

     myFile.delete(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

然後我不得不

// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6' 

添加到我的gradle這個,,可以發現在MVN repository