图像像素操作,错误:java.lang.IllegalArgumentException

时间:2016-03-04 15:17:22

标签: java android bitmap

我正在尝试操纵图像的像素,但它会出错:

$(function() { $("#slider-range-max").slider({ range: "max", min: 0, max: 360, value: 0, slide: function(event, ui) { $("#amount").val(ui.value); $("#amount").css("color", "hsl(" + ui.value + ", 100%, 50%)"); } }); $("#amount").val($("#slider-range-max").slider("value")); });

这是我的代码..

java.lang.IllegalArgumentException: x must be < bitmap.width()

这是logcat错误:

                Uri selectedImg = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImg, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                Toast.makeText(this, "Successfull!", Toast.LENGTH_LONG);
                image = (ImageView)findViewById(R.id.imageView);
                image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                BitmapDrawable abmp = (BitmapDrawable) image.getDrawable();
                bmp = abmp.getBitmap();
                image.setImageBitmap(bmp);

                brightBtn.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        operation = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

                        for (int i = 0; i < bmp.getWidth(); i++) {
                            for (int j = 0; j < bmp.getHeight(); i++) {
                                int p = bmp.getPixel(i, j);
                                int r = Color.red(p);
                                int b = Color.blue(p);
                                int g = Color.green(p);
                                int alpha = Color.alpha(p);

                                r = r + 100;
                                g = g + 100;
                                b = b = 100;
                                alpha = alpha + 100;

                                operation.setPixel(i, j, Color.argb(alpha, r, g, b));
                            }
                        }
                        image.setImageBitmap(operation);
                    }

                });


            }
        }

3 个答案:

答案 0 :(得分:0)

您正在多次创建相同的位图对象并设置为imageview。

您可以多次使用位图对象。无需重新创建对象。

Toast.makeText(this, "Successfull!", Toast.LENGTH_LONG);
image = (ImageView)findViewById(R.id.imageView);
bmp = BitmapFactory.decodeFile(picturePath);
// image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
// BitmapDrawable abmp = (BitmapDrawable) image.getDrawable();
// bmp = abmp.getBitmap();
image.setImageBitmap(bmp);

由于BitmapDrawable中已有原始位图,因此无需再次从Bitmap获取Imageviewbmp

答案 1 :(得分:0)

你通过这条线增加了两次......

for (int j = 0; j < bmp.getHeight(); i++) {

应该是......

for (int j = 0; j < bmp.getHeight(); j++) {

答案 2 :(得分:0)

检查你的循环......第二个循环增加i,但你初始化并使用j的值。我认为这会产生问题。