在bitmap android中的setpixel

时间:2015-07-11 10:28:33

标签: java android bitmap pixels

我有一个位图,我想为位图的每个像素设置一个新的像素值。

这是我的代码:

Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();

int W = bm.getWidth();
int H = bm.getHeight();
int [][][] clr = new int [3][H][W];
int pixel;
for(int i=0;i<W;i++)
    for(int j=0;j<H;j++){
        pixel = bm.getPixel(i, j);
        clr[0][j][i] = Color.red(pixel);
        clr[1][j][i] = Color.green(pixel);
        clr[2][j][i] = Color.blue(pixel);
        }

/*
...
pixel changging process
...
*/

//save a new image
for(int i=0;i<W;i++)
    for(int j=0;j<H;j++)
        bm.setPixel(i, j, Color.rgb(clr[0][j][i],clr[1][j][i],clr[2][j][i]));
saveImageFile(bm);
//end save new image
我该怎么办?保存新图像过程无法工作,我需要一个替代方案来处理这个问题

提前感谢:)

1 个答案:

答案 0 :(得分:0)

此代码正常运行!! 在ImageView中设置新的位图及其工作!

public class Bleach extends AppCompatActivity implements Cloneable {
    Bitmap bm;
    ImageButton bDone;
    Bitmap _bitmapPrev;
    Bitmap _bitmapCur;
    File imageFile;
    Bitmap bm3;
    Bitmap bm4;
    ImageView bl;
    int x;
    int y;

    String[] projection = new String[]{
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.MIME_TYPE
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bleach);
        bDone = (ImageButton) findViewById(R.id.donebtnid);

        bDone.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                finish();
                startActivity(new Intent(Bleach.this, MainMenuActivity.class));
            }
        });

        //bl = (ImageView)findViewById(R.id.pickbleachi        d);



        final Cursor cursor = getContentResolver()
                .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
                        null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                final ImageView bl = (ImageView) findViewById(R.id.pickbleachid);
                String imageLocation = cursor.getString(1);
                imageFile = new File(imageLocation);
                if (imageFile.exists()) {   // TODO: is there a better way to do this?
                    try{
                        bm = BitmapFactory.decodeFile(imageLocation);
                        bm3 = ExifUtil.rotateBitmap(imageLocation,bm);
                    }catch (Exception exception){
                        Log.d("PICtureConfirmation ","bm decodefile"+ exception.getMessage());
                    }
                    try {

                       // bl.setImageBitmap(bm3);
                        //Drawable imgDrawable = teeth.getDrawable();
                        //Bitmap bm5=((BitmapDrawable)bm3.getDrawable()).getBitmap();

                        int W = bm3.getWidth();
                        int H = bm3.getHeight();
                        int [][][] clr = new int [3][H][W];
                        int pixel;
                        for(int i=0;i<W;i++)
                            for(int j=0;j<H;j++){
                                pixel = bm3.getPixel(i, j);
                                clr[0][j][i] = Color.red(pixel+5);
                                clr[1][j][i] = Color.green(pixel+5);
                                clr[2][j][i] = Color.blue(pixel+5);
                            }

/*
...
pixel changging process
...
*/

//save a new image
                        for(int i=0;i<W;i++) {
                            for (int j = 0; j < H; j++) {
                                bm3.setPixel(i, j, Color.rgb(clr[0][j][i], clr[1][j][i], clr[2][j][i]));
                            }
                        }
                        bl.setImageBitmap(bm3);

                    }
                    catch (Exception e){}
                }
            }
        }




    }

    public void SetGamma(double red,double green, double blue){
        Bitmap temp = (Bitmap)_bitmapCur;
        //Bitmap bmap = (Bitmap)temp.clone();
    }
}