1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.media.filterfw.samples.simplecamera;
18
19import android.util.Log;
20
21import java.io.BufferedWriter;
22import java.io.FileWriter;
23import java.io.IOException;
24
25import androidx.media.filterfw.Filter;
26import androidx.media.filterfw.FrameBuffer2D;
27import androidx.media.filterfw.FrameImage2D;
28import androidx.media.filterfw.FrameType;
29import androidx.media.filterfw.FrameValue;
30import androidx.media.filterfw.MffContext;
31import androidx.media.filterfw.OutputPort;
32import androidx.media.filterfw.Signature;
33
34
35public class CSVWriterFilter extends Filter {
36
37    private static final String TAG = "CSVWriterFilter";
38    private static boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
39    private boolean mFirstTime = true;
40    private final static int NUM_FRAMES = 3;
41    private final String mFileName = "/CSVFile.csv";
42
43    public CSVWriterFilter(MffContext context, String name) {
44
45        super(context, name);
46    }
47
48    @Override
49    public Signature getSignature() {
50        FrameType floatT = FrameType.single(float.class);
51        FrameType stringT = FrameType.single(String.class);
52        FrameType floatArrayT = FrameType.array(float.class);
53
54        return new Signature()
55                .addInputPort("sharpness", Signature.PORT_REQUIRED, floatT)
56                .addInputPort("overExposure", Signature.PORT_REQUIRED, floatT)
57                .addInputPort("underExposure", Signature.PORT_REQUIRED, floatT)
58                .addInputPort("colorfulness", Signature.PORT_REQUIRED, floatT)
59                .addInputPort("contrastRating", Signature.PORT_REQUIRED, floatT)
60                .addInputPort("brightness", Signature.PORT_REQUIRED, floatT)
61                .addInputPort("motionValues", Signature.PORT_REQUIRED, floatArrayT)
62                .addInputPort("imageFileName", Signature.PORT_REQUIRED, stringT)
63                .addInputPort("csvFilePath", Signature.PORT_REQUIRED, stringT)
64                .disallowOtherPorts();
65    }
66
67    @Override
68    protected void onProcess() {
69
70
71
72        Log.v(TAG,"in csv writer on process");
73        FrameValue sharpnessValue =
74                getConnectedInputPort("sharpness").pullFrame().asFrameValue();
75        float sharpness = ((Float)sharpnessValue.getValue()).floatValue();
76
77        FrameValue overExposureValue =
78                getConnectedInputPort("overExposure").pullFrame().asFrameValue();
79        float overExposure = ((Float)overExposureValue.getValue()).floatValue();
80
81        FrameValue underExposureValue =
82                getConnectedInputPort("underExposure").pullFrame().asFrameValue();
83        float underExposure = ((Float)underExposureValue.getValue()).floatValue();
84
85        FrameValue colorfulnessValue =
86                getConnectedInputPort("colorfulness").pullFrame().asFrameValue();
87        float colorfulness = ((Float)colorfulnessValue.getValue()).floatValue();
88
89        FrameValue contrastValue =
90                getConnectedInputPort("contrastRating").pullFrame().asFrameValue();
91        float contrast = ((Float)contrastValue.getValue()).floatValue();
92
93        FrameValue brightnessValue =
94                getConnectedInputPort("brightness").pullFrame().asFrameValue();
95        float brightness = ((Float)brightnessValue.getValue()).floatValue();
96
97        FrameValue motionValuesFrameValue =
98                getConnectedInputPort("motionValues").pullFrame().asFrameValue();
99        float[] motionValues = (float[]) motionValuesFrameValue.getValue();
100        float vectorAccel = (float) Math.sqrt(Math.pow(motionValues[0], 2) +
101                Math.pow(motionValues[1], 2) + Math.pow(motionValues[2], 2));
102
103        FrameValue imageFileNameFrameValue =
104                getConnectedInputPort("imageFileName").pullFrame().asFrameValue();
105        String imageFileName = ((String)imageFileNameFrameValue.getValue());
106
107        FrameValue csvFilePathFrameValue =
108                getConnectedInputPort("csvFilePath").pullFrame().asFrameValue();
109        String csvFilePath = ((String)csvFilePathFrameValue.getValue());
110
111
112        if(mFirstTime) {
113            try {
114                FileWriter fileWriter = new FileWriter(csvFilePath + "/CSVFile.csv");
115                BufferedWriter csvWriter = new BufferedWriter(fileWriter);
116
117                csvWriter.write("FileName,Sharpness,OverExposure,UnderExposure,Colorfulness," +
118                            "ContrastRating,Brightness,Motion");
119                csvWriter.newLine();
120                csvWriter.close();
121            } catch (IOException e) {
122                // TODO Auto-generated catch block
123                e.printStackTrace();
124            }
125            mFirstTime = false;
126        }
127
128        try {
129            Log.v(TAG,"about to write to file");
130            FileWriter fileWriter = new FileWriter(csvFilePath + mFileName, true);
131            BufferedWriter csvWriter = new BufferedWriter(fileWriter);
132
133            csvWriter.write(imageFileName + "," + sharpness + "," + overExposure + "," +
134                    underExposure + "," + colorfulness + "," + contrast + "," + brightness +
135                    "," + vectorAccel);
136            Log.v(TAG, "" + imageFileName + "," + sharpness + "," + overExposure + "," +
137                    underExposure + "," + colorfulness + "," + contrast + "," + brightness +
138                    "," + vectorAccel);
139            csvWriter.newLine();
140            csvWriter.close();
141        } catch (IOException e) {
142            // TODO Auto-generated catch block
143            e.printStackTrace();
144            throw new RuntimeException(e);
145        }
146    }
147}
148