error with setPixels

前端 未结 5 1655
清歌不尽
清歌不尽 2020-12-17 08:48

i am trying to edit images. but i am getting errors with setPixels.

        picw = pic.getWidth();
        pich = pic.getHeight();
        picsize = picw*pic         


        
相关标签:
5条回答
  • 2020-12-17 09:12

    Most probably your pic is immutable. By default, any bitmap created from drawable would be immutable.

    If you need to modify an existing bitmap, you should do following:

    // Create a bitmap of the same size
    Bitmap newBmp = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Config.ARGB);
    // Create a canvas  for new bitmap
    Canvas c = new Canvas(newBmp); 
    
    // Draw your old bitmap on it. 
    c.drawBitmap(pic, 0, 0, new Paint());
    
    0 讨论(0)
  • 2020-12-17 09:20

    It's simple, just use the following command to change it to a mutable Bitmap:

    myBitmap = myBitmap.copy( Bitmap.Config.ARGB_8888 , true);
    

    Now the Bitmap myBitmap is replaced by the same Bitmap but this time is mutable

    You can also choose another way of storing Pixels (ARGB_8888 etc..): https://developer.android.com/reference/android/graphics/Bitmap.Config.html

    0 讨论(0)
  • 2020-12-17 09:34

    I was facing this problem and finally fixed after long time.

    public static void filterApply(Filter filter){
        Bitmap bitmcopy = PhotoModel.getInstance().getPhotoCopyBitmap();
        //custom scalling is important to apply filter otherwise it will not apply on image
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmcopy, bitmcopy.getWidth()-1, bitmcopy.getHeight()-1, false);
        filter.processFilter(scaledBitmap);
        filterImage.setImageBitmap(scaledBitmap);
    }
    
    0 讨论(0)
  • 2020-12-17 09:37

    I had the same problem. Use to fix it:

    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;
    Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.my_bitmap, opt );
    
    0 讨论(0)
  • 2020-12-17 09:38

    I think your Bitmap is not mutable (see setPixel()'s documentation).

    If so, create a mutable copy of this Bitmap (using Bitmap.copy(Bitmap.Config config, boolean isMutable) as an example) and work on this one.

    0 讨论(0)
提交回复
热议问题