2015-07-11 93 views
0

我有一個位圖,我想爲位圖上的每個像素設置一個新的像素值。setpixel in bitmap android

這裏是我的代碼:

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 

那我該怎麼辦?保存新的圖像處理無法工作,我需要一個替代品在這個問題上

由於事先工作:)

+0

您的位圖應該是可變的,以便對其進行更改。你應該創建新的可變位圖。像這樣:位圖bm = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); – AterLux

+0

通過創建一個新的位圖變量解決,謝謝你的幫助@AterLux有一個美好的一天:) –

回答

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(); 
    } 
}