Search code examples
androidfileandroid-camera2

Writing all Camera2 CaptureResults into a file


I am trying to export into a texte file all the parameters from an acquisition using Camera2 API on Android. I figured out that they are all contained in the CaptureCallback but I just don't know how to parse them all and write them into a file.

private CameraCaptureSession.CaptureCallback mCaptureCallback  = new CameraCaptureSession.CaptureCallback()
{

    private void process(CaptureResult result)
    {
        //parse and write as txt all available result keys and values
    }
}

It is probably an easy thing to do.


Solution

  • Here is a piece of code that do the job. I open a file, parse all the keys and write the values into it :

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_PICTURES), "MyApp");
    File outputfile = new File(mediaStorageDir, "Camera_parameters.txt");
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return;
        }
    }
    try {
        FileOutputStream m_outputstream = new FileOutputStream(outputfile);
        OutputStreamWriter m_stream = new OutputStreamWriter(m_outputstream);
        List<CaptureResult.Key<?>> keys = result.getKeys();
        for (CaptureResult.Key<?> key : keys) {
            Log.d(TAG, key + " has value " + result.get(key));
            m_stream.write(key + "\t" + result.get(key) + "\n");
        }
        m_stream.close();
    }
    catch (IOException e)
    {
        Log.e("Exception", "File write failed: " + e.toString());
    }