Question

What I am trying to do, is to take a photo using a camera intent, retrieve and convert said photo to a grayscale byte array (note: I am not interested in getting a grayscale image, just need the byte data). Then finally, apply a threshold and average all the pixels above the threshold.

The relevant snippet of code is:

@Override
        public void onClick(View v) {



            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);              
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);


            }

    });

}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    InputStream stream = null;
        if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            try {
                stream = getContentResolver().openInputStream(data.getData());
                bmp = BitmapFactory.decodeStream(stream);

                bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
                for(int x = 0; x < bmp.getWidth(); ++x) {
                    for(int y = 0; y < bmp.getHeight(); ++y) {
                        int index = y * bmp.getWidth() + x;
                        int R = (pixels[index] >> 16) & 0xff;
                        int G = (pixels[index] >> 8) & 0xff;
                        int B = (pixels[index]) & 0xff;

                        double Grey = (0.299 * R + 0.587 * G + 0.114 * B);

                        if(Grey > 20) {
                            sum += Grey;
                            count++;
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (stream != null)
                    try {
                        stream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

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


                            double Y = sum / count;

The toast comment is there for testing, I used that earlier to make sure the intent was working - it was, but the path it gave was

Content://media/external/images/media/##

(where ## is the next photo number).

I have tried this in the Eclipse emulator, and I get a RuntimeException error at where the bitmap starts. I get a similar crash when I do a live test on an LG Optimus L3 (Android version 2.3.6).

I am convinced I have goofed up somewhere in the bitmap part of the code (and yes, I have read the developers guide and several threads here and in other places). What is going wrong with the bitmap part?

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top