在带有过滤器的应用中,我的代码存在一些问题。如以下代码所示,当同时使用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.
有什么主意吗?预先谢谢你!
答案 0 :(得分:0)
这是因为您正在将一个不变的位图传递给Canvas。为了通过画布对位图执行操作,它必须是可变的。 像这样初始化位图:
Bitmap immutableBitmap = Bitmap.createBitmap(src, 0, (int)(height * 0.001), width, (int)(height * 0.999));
Bitmap mutableBitmap = immutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(mutableBitmap );
答案 1 :(得分:0)
抱歉,一个月没有回复,但我正在休假:)
无论如何,我终于在代码中发现了这个错误……一个非常愚蠢的错误:
当我写信时:
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;
谢谢!