I am attempting to analyze an array of bitmaps and follow the position of the brightest spot through each of the bitmaps. After analyzing each bitmap, I want to find the x and y position of the brightest(which I just set to be the average of the RGB values) spot and then graph that position on a graphview, and graph all max x,y values from each bitmap on the same graphview. Here is my code:
public class MainActivity extends AppCompatActivity {
public static final int IMAGE_GALLERY_REQUEST = 20;
LineGraphSeries<DataPoint> series;
private int pixels[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GraphView graph = (GraphView) findViewById(R.id.graph);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(720);
graph.getViewport().setMinY(0);
graph.getViewport().setMaxY(1280);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setXAxisBoundsManual(true);
}
public void onImageGalleryClicked(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
//where do we find the data
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
String pictureDirectoryPath = pictureDirectory.getPath();
//get a URI representation so android can deal with it
Uri data = Uri.parse(pictureDirectoryPath);
//now created data, need to set data(where to look for it) and also what media we want to look for
photoPickerIntent.setDataAndType(data, "video/*");
//for any type just use audio/* for any type
//can be any number for startActivity, just needs to be a number not previously used. this gives it a unique ID so we know which is which
startActivityForResult(photoPickerIntent, IMAGE_GALLERY_REQUEST);
//created random variable so that it is not previously used
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_GALLERY_REQUEST) {
Uri imageUri = data.getData();
//declare a stream to read the image data from the SD card
InputStream inputStream;
try {
inputStream = getContentResolver().openInputStream(imageUri);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
GraphView graph = (GraphView) findViewById(R.id.graph);
Uri videouri = data.getData();
retriever.setDataSource(this, videouri);
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong(time);
long seconds = timeInmillisec / 1000;
ArrayList<Bitmap> mylist = new ArrayList<Bitmap>();
for (int i = 0; i < seconds; i++) {
Bitmap bArray = retriever.getFrameAtTime(1000000 * i, MediaMetadataRetriever.OPTION_CLOSEST);
mylist.add(bArray);
BitmapFactory.Options options = new BitmapFactory.Options();
}
series = new LineGraphSeries<DataPoint>();
for (int r = 0; r < mylist.size(); r++) {
Bitmap newbitmap = mylist.get(r);
int w = newbitmap.getWidth();
int h = newbitmap.getHeight();
pixels = new int[4 * w * h];
int a = 20; //this is the width of the smaller image that we are cropping to sum up
int b = 20; ////this is the length(height) of the smaller image that we are cropping to sum up
//getPixels() returns the complete int[] array of the source bitmap, so has to be initialized with the same length as the source bitmap's height x width.
int tempi = 0;
int tempj = 0;
double tempmax = 0;
double sum = 0;
for (int i = 0; i < h - b; i++) {
for (int j = 0; j < w - a; j++) {
newbitmap.getPixels(pixels, 0, w, j, i, a, b);
//summing code
for (int e = 0; e < a * b; e++) {
int red = (pixels[e] >> 16) & 0xff;
int green = (pixels[e] >> 8) & 0xff;
int blue = pixels[e] & 0xff;
double grey = (red + green + blue) / 3;
sum = sum + grey;
}
if (sum > tempmax) {
tempmax = sum;
tempi = i;
tempj = j;
}
sum = 0;
}
}
series.appendData(new DataPoint(tempj, tempi), true, mylist.size());
graph.addSeries(series);
tempi = 0;
tempj = 0;
tempmax = 0;
}
//picture width = 720 dp
//picture height = 1280 dp
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show();
}
}
}
}
}
And here is the error I am getting
02-26 00:36:23.946 24443-24443/com.example.garrett.imageprocessingv4 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.garrett.imageprocessingv4, PID: 24443 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=20, result=-1, data=Intent { dat=content://media/external/video/media/275 flg=0x1 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) }} to activity {com.example.garrett.imageprocessingv4/com.example.garrett.imageprocessingv4.MainActivity}: java.lang.IllegalArgumentException: new x-value must be greater then the last value. x-values has to be ordered in ASC. at android.app.ActivityThread.deliverResults(ActivityThread.java:4556) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4599) at android.app.ActivityThread.-wrap22(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1715) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6823) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) Caused by: java.lang.IllegalArgumentException: new x-value must be greater then the last value. x-values has to be ordered in ASC. at com.jjoe64.graphview.series.BaseSeries.checkValueOrder(BaseSeries.java:486) at com.jjoe64.graphview.series.BaseSeries.appendData(BaseSeries.java:408) at com.jjoe64.graphview.series.LineGraphSeries.appendData(LineGraphSeries.java:646) at com.jjoe64.graphview.series.BaseSeries.appendData(BaseSeries.java:464) at com.example.garrett.imageprocessingv4.MainActivity.onActivityResult(MainActivity.java:123) at android.app.Activity.dispatchActivityResult(Activity.java:7322) at android.app.ActivityThread.deliverResults(ActivityThread.java:4552) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4599) at android.app.ActivityThread.-wrap22(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1715) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6823) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)