出ているエラー
Androidstudioで二つの画像の差分部分を結果画像として出力するアプリを作成していて、ImageViewで表示している二つの画像の大きさが違う時、解析前に画像2を伸縮して画像1に揃えるということをしたいのですが、上手くいきません。
解析ボタン→大きさが違えば修正という流れになって欲しいですのですが、Android実機の方でボタンを押した際に「アプリが停止しました」とエラーが出ます。logcatのエラーは下にあります
コード説明
画像の大きさが違うパターンと合っているパターンでifelse分けしていて、
ImageVieとして取り込み、処理しているのですが、画像1をbitmap1、画像2をbitmap2、結果画像をbitmap3、画像2を修正した結果をbitmap4として処理しています。
public void onCameraDevitionClick(View view) { ImageView src_img1 = (ImageView) findViewById(R.id.src_img1); ImageView src_img2 = (ImageView) findViewById(R.id.src_img2); ImageView result_img = (ImageView) findViewById(R.id.result_img); Bitmap bitmap1,bitmap2; Bitmap bitmap3; if (src_img1.getDrawable() instanceof BitmapDrawable) { bitmap1 = ((BitmapDrawable) src_img1.getDrawable()).getBitmap(); } else { Drawable d = src_img1.getDrawable(); bitmap1 = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap1); d.draw(canvas); } if (src_img2.getDrawable() instanceof BitmapDrawable) { bitmap2 = ((BitmapDrawable) src_img2.getDrawable()).getBitmap(); } else { Drawable d = src_img2.getDrawable(); bitmap2 = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap2); d.draw(canvas); } if (bitmap1.getWidth() != bitmap2.getWidth()|| bitmap1.getHeight() != bitmap2.getHeight()) { int wbmp1 = bitmap1.getWidth(); int wbmp2 = bitmap2.getWidth(); int hbmp2 = bitmap2.getWidth(); int hbmp1 = bitmap1.getHeight(); float t = wbmp1 / wbmp2; float t2 = hbmp1 / hbmp2; Bitmap bitmap4 = Bitmap.createBitmap(wbmp1, hbmp1, Bitmap.Config.ARGB_8888); // for (int i = 0; i < wbmp1; i++) { // for (int j = 0; j < hbmp1; j++) { for (int i = 0; i < wbmp2; i++) { for (int j = 1; j < hbmp2; j++) { int pbmp2 = bitmap2.getPixel(i, j); if((int)(i * t)<=wbmp1&&(int)(j*t2)<=hbmp1) bitmap4.setPixel( (int)(i * t), (int)(j * t2), pbmp2); } } src_img2.setImageDrawable(null); src_img2.setImageBitmap(bitmap4); int number=numPicker0.getValue(); bitmap3= Bitmap.createBitmap(wbmp1,hbmp1, Bitmap.Config.ARGB_8888); // for (int i = 0; i < wbmp1; i++) { // for (int j = 0; j < hbmp1; j++) { for (int i = 0; i < wbmp1; i++) { for (int j = 1; j < hbmp1; j++) { int pbmp1=bitmap1.getPixel(i, j); int pbmp4=bitmap4.getPixel(i, j); int k=(pbmp1-pbmp4)/100000; //if (k<=9&&k>=-9){ if (k<=number&&k>=-number){ // Do whatever you want Correct Image.. // both image Are Same bitmap3.setPixel(i, j, pbmp1); } else { // both image Are diffrent // bitmap3.setPixel(i,j,-16777216); bitmap3.setPixel(i,j,color); } } } result_img.setImageBitmap(bitmap3); } else { int number=numPicker0.getValue(); int wbmp1=bitmap1.getWidth(); int hbmp1=bitmap1.getHeight(); bitmap3= Bitmap.createBitmap(wbmp1,hbmp1, Bitmap.Config.ARGB_8888); // for (int i = 0; i < wbmp1; i++) { // for (int j = 0; j < hbmp1; j++) { for (int i = 0; i < wbmp1; i++) { for (int j = 1; j < hbmp1; j++) { int pbmp1=bitmap1.getPixel(i, j); int pbmp2=bitmap2.getPixel(i, j); int k=(pbmp1-pbmp2)/100000; if (k<=number&&k>=-number){ // Do whatever you want Correct Image.. // both image Are Same bitmap3.setPixel(i, j, pbmp1); } else { // both image Are diffrent // bitmap3.setPixel(i,j,-16777216); bitmap3.setPixel(i,j,color); } } } result_img.setImageBitmap(bitmap3); } }
logcatでの表示1部
DeclaredOnClickListener.onClick(View.java:5374) at android.view.View.performClick
と表示していますがOnClickListenerがいまいちエラーの何に関係しているのか分からず、Handler.javaにLooper.javaがバッグエンドで処理を渡していることなどみたのですが、https://academy.realm.io/jp/posts/android-thread-looper-handler/結局何がか関係があるのか分かりませんでした。
atty: uid=1000(system) ActivityManager expire 14 lines 2021-09-23 09:37:16.428 1412-3166/? I/chatty: uid=1000(system) Binder:1412_9 expire 1 line 2021-09-23 09:37:16.690 1412-12661/? I/chatty: uid=1000(system) Binder:1412_12 expire 2 lines 2021-09-23 09:37:17.031 1412-6152/? I/chatty: uid=1000(system) Binder:1412_F expire 3 lines 2021-09-23 09:37:17.032 1412-2504/? I/chatty: uid=1000(system) Binder:1412_4 expire 1 line 2021-09-23 09:37:17.075 1412-2559/? I/chatty: uid=1000(system) Binder:1412_6 expire 1 line 2021-09-23 09:37:17.548 1412-12673/? I/chatty: uid=1000(system) Binder:1412_18 expire 1 line 2021-09-23 09:37:18.983 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:37:18.983 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:37:27.645 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:37:27.649 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:37:37.437 332-332/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.websarva.wings.android.cameraintentsample, PID: 332 java.lang.IllegalStateException: Could not execute method for android:onClick at android.view.View$DeclaredOnClickListener.onClick(View.java:5374) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24774) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6518) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.view.View$DeclaredOnClickListener.onClick(View.java:5369) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24774) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6518) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.IllegalArgumentException: y must be < bitmap.height() at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:1677) at android.graphics.Bitmap.getPixel(Bitmap.java:1622) at com.websarva.wings.android.cameraintentsample.UseCameraActivity.onCameraDevitionClick( UseCameraActivity.java:276) at java.lang.reflect.Method.invoke(Native Method) at android.view.View$DeclaredOnClickListener.onClick(View.java:5369) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24774) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6518) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 2021-09-23 09:37:37.450 1412-1541/? I/chatty: uid=1000(system) Binder:1412_1 expire 4 lines 2021-09-23 09:37:48.161 1412-3193/? I/chatty: uid=1000(system) Binder:1412_A expire 1 line 2021-09-23 09:37:59.193 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:37:59.197 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:38:16.895 1412-1543/? I/chatty: uid=1000(system) Binder:1412_2 expire 1 line 2021-09-23 09:38:32.054 1412-1973/? I/chatty: uid=1000(system) batterystats-wo expire 12 lines 2021-09-23 09:39:36.875 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:39:36.876 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:41:11.683 1412-1973/? I/chatty: uid=1000(system) batterystats-wo expire 20 lines 2021-09-23 09:41:11.706 1412-1929/? I/chatty: uid=1000(system) ActivityManager expire 10 lines 2021-09-23 09:44:09.822 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:44:09.823 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:46:49.842 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:46:49.845 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:47:55.616 1412-1973/? I/chatty: uid=1000(system) batterystats-wo expire 12 lines 2021-09-23 09:48:07.046 1412-2201/? I/chatty: uid=1000(system) ranker expire 1 line 2021-09-23 09:49:54.493 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:49:54.496 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:50:03.805 1412-1929/? I/chatty: uid=1000(system) ActivityManager expire 13 lines 2021-09-23 09:52:34.504 661-661/? I/wificond: Enters wifigbk_ageOutHistory 2021-09-23 09:52:34.506 661-661/? I/wificond: Enters wifigbk_ageOutHistory
あなたの回答
tips
プレビュー