我在一个带有过滤器的应用程序中的代码有一些问题。当将createBitmap与drawBitmap一起使用时,会出现崩溃,如我在以下代码中所示:
public static Bitmap doFilter (Bitmap src) {
int width, height;
height = src.getHeight();
width = src.getWidth();
float[] transfMatrix = {
1.5f, 0, 0, 0, 0,
0, 1.5f, 0, 0, 0,
0, 0, 1.5f, 0, 0,
0, 0, 0, 1, 0};
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0f);
cm.set(transfMatrix);
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
Paint paint = new Paint();
paint.setColorFilter(cf);
Bitmap bmOut = Bitmap.createBitmap(src);
Canvas c = new Canvas(bmOut);
c.drawBitmap(bmOut, 0, 0, paint);
return bmOut;我已经尝试了如下编码,但我不想裁剪图像,如果可能的话…
public static Bitmap doFilter (Bitmap src) {
int width, height;
height = src.getHeight();
width = src.getWidth();
float[] transfMatrix = {
1.5f, 0, 0, 0, 0,
0, 1.5f, 0, 0, 0,
0, 0, 1.5f, 0, 0,
0, 0, 0, 1, 0};
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0f);
cm.set(transfMatrix);
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
Paint paint = new Paint();
paint.setColorFilter(cf);
Bitmap bmOut = Bitmap.createBitmap(src, 0, (int)(height * 0.001), width, (int)(height * 0.999));
Canvas c = new Canvas(bmOut);
c.drawBitmap(bmOut, 0, 0, paint);
return bmOut;关于这里的Logcat,你可以找到失败的部分,但我不太理解所有这些消息……
07-21 16:08:05.790 23134-23134/? E/Diag_Lib: Diag_LSM_Init: Failed to open handle to diag driver, error = 2
07-21 16:08:05.790 23134-23134/? E/Sensors: sns_fsa_la.c(386):fsa: fflush failed, 9
07-21 16:08:05.791 23134-23134/? E/Sensors: sns_fsa_la.c(386):fsa: fflush failed, 9
07-21 16:08:05.815 23134-23140/? W/Sensors: sns_smr_la.c(446):smr_la: smr_apps_la_thread_main is starting, fd=11, sns_smr.en_rx_msg_ptr=b6f73a04
07-21 16:08:05.828 23134-23142/? W/Sensors: sns_sam_app.c(6827):sns_sam_reg_algo: Registering algo service 16, err 0
07-21 16:08:05.842 23134-23144/? E/Sensors: sns_debug_main.c(565):Debug Config File missing in EFS!
07-21 16:08:07.306 22240-22473/? E/FA: Missing google_app_id. Firebase Analytics disabled.
07-21 16:08:07.854 22240-22240/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: v1_0_0, PID: 22240
java.lang.RuntimeException: Unable to start activity ComponentInfo{v1_0_0/v1_0_0.Edition}: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
at android.graphics.Canvas.<init>(Canvas.java:142)
at v1_0_0.Edition.doFilter(Edition.java:300)
at v1_0_0.Edition.initViews(Edition.java:235)
at v1_0_0.Edition.onCreate(Edition.java:99)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-21 16:08:07.867 595-2137/? W/ActivityManager: Force finishing activity v1_0_0/.Edition
07-21 16:08:07.871 595-2137/? W/ActivityManager: Force finishing activity v1_0_0/.Preview
07-21 16:08:07.978 1468-1640/? W/awmy: Empty context buffer. Thus might mean that the context is not synced down.有什么想法吗?提前谢谢你!
发布于 2019-08-24 00:23:45
很抱歉没有在一个月内回复,但我正在度假:)
不管怎么说,我终于发现了代码中的错误...一个非常愚蠢的错误:
当我写道:
c.drawBitmap(bmOut, 0, 0, paint);我应该放入原始的位图,而不是输出的位图:
c.drawBitmap(src, 0, 0, paint);所以最后的代码,稍微重新组织一下...
public static Bitmap doFilter (Bitmap src) {
float[] transfMatrix = {
1.5f, 0, 0, 0, 0,
0, 1.5f, 0, 0, 0,
0, 0, 1.5f, 0, 0,
0, 0, 0, 1, 0};
ColorMatrix cm = new ColorMatrix();
cm.postConcat(new ColorMatrix(transfMatrix));
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
Canvas c = new Canvas(bmOut);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
c.drawBitmap(src, 0, 0, paint);
return bmOut;谢谢大家!
发布于 2019-07-21 23:08:00
这是因为您正在将一个不可变的位图传递给画布。为了通过画布对位图执行操作,它需要是可变的。按如下方式初始化位图:
Bitmap immutableBitmap = Bitmap.createBitmap(src, 0, height, width, height);
Bitmap mutableBitmap = immutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(mutableBitmap );https://stackoverflow.com/questions/57062490
复制相似问题