使用getPixel和setPixel将图像转换为灰度

时间:2014-11-19 09:33:29

标签: java android image-processing

我是编程新手,我需要知道如何使用getpixels将我拥有的图像转换为灰度.Pls帮助我:(我需要从我的图库中获取图像,将其转换为位图并开始执行图像处理。我只有2天的时间来提交。我真的需要帮助才能使用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;   

                }        
            }

1 个答案:

答案 0 :(得分:0)

我认为错误The primitive type int of x does not have a field y的原因是你的result数组初始化。 你应该写道:

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

而不是int result[];并使用result[x][y]访问该元素。