【发布时间】:2013-05-16 08:38:43
【问题描述】:
我试过这段代码,这里将imageView转换为位图图像,然后将其转换为灰度。但是我们想访问像素值,但我们将所有像素值都设为 0,请有人帮助我们
public Bitmap toGrayscale(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
image=toGrayscale(bmap);
ImageView imageView1 = (ImageView) findViewById(R.id.imgView1);
imageView1.setImageBitmap(image);
pixels = new int[image.getWidth()*image.getHeight()];
image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
bu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text3.setText(String.valueOf(pixels[m++]));
}
});
}
【问题讨论】:
-
你能显示更多的代码吗?您的位图“图像”和整数“m”在哪里初始化?你有什么错误吗?
-
谢谢,现在我添加了额外的代码,这里从图库中加载图像。并且 m 在与 public 相同的类中初始化...
标签: java android image get pixels