简体   繁体   中英

Using getPixel and setPixel to convert image into grayscale

I am new to programming and I need to know how do I use getpixels to convert the image I have into grayscale.Pls help me: ( I need to get an image from my gallery, convert it into bitmap and start doing image processing. I only have 2 days left for my submission. I really need help with using getPixels.

public class SelectImageActivity extends Activity implements OnClickListener {

    // Image loading result to pass to startActivityForResult method.
    private static int LOAD_IMAGE_RESULTS = 1;

    // GUI components
    private Button button;  // The button
    private ImageView image;// ImageView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_image);

        // Find references to the GUI objects
        button = (Button)findViewById(R.id.button);
        image = (ImageView)findViewById(R.id.image);

        // Set button's onClick listener object.
        button.setOnClickListener(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Here we need to check if the activity that was triggers was the Image Gallery.
        // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
        // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
        if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            Bitmap bitmap1=BitmapFactory.decodeFile(imagePath);
            int threshold=50;
            int result[];

            for (int x=0; x<bitmap1.getWidth();x++)
            {
                for (int y=0; y<bitmap1.getHeight();y++)
                { 
                    if (bitmap1.getPixel(x, y)>threshold)

                        result[x,y]=255; () //((I have a error here saying "The primitive type int of x does not have a field y"))
                    else 
                        result[x,y]=0;   

                }        
            }

I think the reason for error The primitive type int of x does not have a field y is your result array initialization. You should wrote :

int[][] result = new int[bitmap1.getWidth()][bitmap1.getHeight()];

instead of int result[]; and access the element using result[x][y] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM