bitmap crashing when trying to convert camera-intent image to grayscale byte array

ⅰ亾dé卋堺 提交于 2019-12-02 10:58:41

After a bit of research throughout some questions here (and I upvoted the ones that were especially useful), and in various coding places and quite a bit of late-night-self-coding-education, I have it working now. Below is the working code snippet:

@Override
        public void onClick(View v) {



            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            //intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);                
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

            }

    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            final ContentResolver cr = getContentResolver();
                    final String[] p1 = new String[] {
                            MediaStore.Images.ImageColumns._ID,
                            MediaStore.Images.ImageColumns.DATE_TAKEN
                    };
                    Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
                    if ( c1.moveToFirst() ) {
                        String uristringpic = "content://media/external/images/media/" +c1.getInt(0);
                        Uri uri = Uri.parse(uristringpic);
                        try {
                            Bitmap bm = android.provider.MediaStore.Images.Media.getBitmap(cr, uri);
                            int w = bm.getWidth();
                            int h = bm.getHeight();

                            Bitmap bmg = Bitmap.createBitmap(w, h, bm.getConfig());

                            for(int x = 0; x < w; ++x) {
                                for(int y = 0; y < h; ++y) {
                                    int pixel = bm.getPixel(x, y);
                                    a = Color.alpha(pixel);
                                    r = Color.red(pixel);
                                    g = Color.green(pixel);
                                    b = Color.blue(pixel);
                                    r = g = b = (int)(0.299 * r + 0.587 * g + 0.114 * b);

                                    bmg.setPixel(x, y, Color.argb(a, r, g, b));
                                    grey = 0.299 * r + 0.587 * g + 0.114 * b;
                                    if(grey > 20) {
                                        sum += grey;
                                        count++;

                                    } 
                                    //int grey = (r + g + b) / 3;
                                }
                            }


                            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
                            imageView.setImageBitmap(bmg);
                            //Toast.makeText(MainActivity.this, String.valueOf(Y), Toast.LENGTH_LONG).show();
                            //Toast.makeText(this, uri.toString(),Toast.LENGTH_LONG).show();


                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                        }
                        //Toast.makeText(this, "newuri " + uri, Toast.LENGTH_LONG).show();
                    } c1.close();





                //Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show(); 


                            double Y = sum / count;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!