Java 向图形视图添加更改的值

Java 向图形视图添加更改的值,java,android,android-graphview,Java,Android,Android Graphview,我试图分析一组位图,并跟踪每个位图中最亮点的位置。在分析每个位图之后,我想找到最亮点的x和y位置(我刚刚设置为RGB值的平均值),然后在graphview上绘制该位置,并在同一graphview上绘制每个位图的所有最大x、y值。这是我的密码: public class MainActivity extends AppCompatActivity { public static final int IMAGE_GALLERY_REQUEST = 20; LineGraphSeries<Dat

我试图分析一组位图,并跟踪每个位图中最亮点的位置。在分析每个位图之后,我想找到最亮点的x和y位置(我刚刚设置为RGB值的平均值),然后在graphview上绘制该位置,并在同一graphview上绘制每个位图的所有最大x、y值。这是我的密码:

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();

            }

        }
    }

}
public类MainActivity扩展了AppCompatActivity{
公共静态最终int图像\u画廊\u请求=20;
线条系列;
私有整数像素[];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GraphView图形=(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);
}
已单击图像库上的公共无效(视图v){
意向照片采集器内容=新意向(意向.行动\u采集器);
//我们在哪里找到数据
文件pictureDirectory=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u MOVIES);
字符串pictureDirectory路径=pictureDirectory.getPath();
//获取URI表示,以便android能够处理它
Uri数据=Uri.parse(PictureDirectory路径);
//现在创建了数据,需要设置数据(在哪里查找数据)以及我们要查找的媒体
photoPickerIntent.setDataAndType(数据为“video/*”);
//对于任何类型,只需对任何类型使用audio/*即可
//可以是startActivity的任何数字,只需要是以前没有使用过的数字。这会给它一个唯一的ID,以便我们知道哪个是哪个
startActivityForResult(照片采集器、图像库请求);
//创建随机变量,以便以前不使用它
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(resultCode==RESULT\u OK){
if(requestCode==图像\u画廊\u请求){
Uri imageUri=data.getData();
//声明一个流以从SD卡读取图像数据
输入流输入流;
试一试{
inputStream=getContentResolver().openInputStream(imageUri);
MediaMetadataRetriever retriever=新的MediaMetadataRetriever();
GraphView图形=(GraphView)findViewById(R.id.graph);
Uri videouri=data.getData();
setDataSource(这个,videouri);
字符串时间=retriever.extractMetadata(MediaMetadataRetriever.METADATA\u KEY\u DURATION);
long-timeInmillisec=long.parseLong(时间);
长秒=时间单位毫秒/1000;
ArrayList mylist=新的ArrayList();
对于(int i=0;i>16)和0xff;
绿色整数=(像素[e]>>8)&0xff;
蓝色整数=像素[e]&0xff;
双灰=(红+绿+蓝)/3;
总和=总和+灰色;
}
如果(总和>最大温度){
tempmax=总和;
tempi=i;
tempj=j;
}
总和=0;
}
}
appendData(新数据点(tempj,tempi),true,mylist.size();
图.addSeries(系列);
tempi=0;
tempj=0;
tempmax=0;
}
//图片宽度=720 dp
//图片高度=1280 dp
}catch(filenotfounde异常){
e、 printStackTrace();
Toast.makeText(这是“无法打开图像”,Toast.LENGTH_LONG.show();
}
}
}
}
}

这是我得到的错误

02-26 00:36:23.946 24443-24443/com.example.garrett.imageprocessingv4 E/AndroidRuntime:致命异常:main 进程:com.example.garrett.imageprocessingv4,PID:24443 java.lang.RuntimeException:传递结果失败