我正在尝试编辑图像。但我收到了setPixels的错误。
picw = pic.getWidth();
pich = pic.getHeight();
picsize = picw*pich;
int[] pix = new int [picsize];
pic.getPixels(pix, 0, picw, 0, 0, picw, pich);
pic.setPixels(pix,0,pic.getWidth(),0,0,pic.getWidth(),pic.getHeight());
但是我使用setPixels
获得非法状态异常Caused by: java.lang.IllegalStateException
at android.graphics.Bitmap.setPixels(Bitmap.java:878)
at com.sandyapps.testapp.testapp.onCreate(testapp.java:66)
答案 0 :(得分:60)
我认为您的Bitmap
不可变(请参阅setPixel()'s documentation)。
如果是这样,请创建此Bitmap的可变副本(以Bitmap.copy(Bitmap.Config config, boolean isMutable)
为例)并对此进行处理。
答案 1 :(得分:6)
很可能你的pic
是不可变的。默认情况下,从drawable创建的任何位图都是不可变的。
如果您需要修改现有的位图,您应该执行以下操作:
// 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());
答案 2 :(得分:4)
这很简单,只需使用以下命令将其更改为可变位图:
myBitmap = myBitmap.copy( Bitmap.Config.ARGB_8888 , true);
现在,位图myBitmap
被相同的位图替换,但这次是可变的
您还可以选择另一种存储像素的方式(ARGB_8888等..): https://developer.android.com/reference/android/graphics/Bitmap.Config.html
答案 3 :(得分:0)
我有同样的问题。用于修复它:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.my_bitmap, opt );
答案 4 :(得分:0)
我遇到了这个问题,经过很长时间终于解决了。
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);
}