2016-11-13 2 views
0

Android vergleichen 2 Bilder Bitmap-Code zu verwenden und zu Obst Kategorie zu sagen, ist Obst (Apfel/Banane) oder nicht Frucht.Android vergleichen 2 Bilder und sagen zu Obst Kategorie zu ist Obst (Apfel/Banane) oder nicht Obst

Ich habe ein Problem mit Bitmap und BitmapFactory zu vergleichen, Laufzeitfehler zu haben, ich habe eine Lösung für das Problem.

  1. activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    tools:context=".MainActivity" > 
 

 
    <ImageView 
 
     android:id="@+id/imageView" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:layout_centerHorizontal="true" /> 
 

 
    <Button 
 
     android:id="@+id/buttonIntent" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_alignParentTop="true" 
 
     android:text="Gallery" /> 
 

 
    <Button 
 
     android:text="OK to Search" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBottom="@+id/buttonIntent" 
 
     android:layout_alignParentRight="true" 
 
     android:layout_alignParentEnd="true" 
 
     android:id="@+id/button" /> 
 

 
</RelativeLayout>

  1. activity_main2.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:orientation="vertical" android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 

 
    <TextView 
 
     android:text="Have fruit as Apple" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerVertical="true" 
 
     android:layout_centerHorizontal="true" 
 
     android:id="@+id/textView" 
 
     android:textSize="20sp" 
 
     android:textColor="@android:color/black" /> 
 

 
</RelativeLayout>

  1. MainActivity.java

import android.graphics.Bitmap; 
 
import android.graphics.BitmapFactory; 
 
import android.net.Uri; 
 
import android.os.Bundle; 
 
import android.app.Activity; 
 
import android.content.Intent; 
 
import android.provider.MediaStore; 
 
import android.view.View; 
 
import android.widget.Button; 
 
import android.widget.ImageView; 
 

 
import java.io.FileNotFoundException; 
 
import java.io.IOException; 
 

 
public class MainActivity extends Activity { 
 
    public static final int REQUEST_GALLERY = 1; 
 

 
    // Code to compare picture. 
 
    Bitmap bitmap; // Picture with select file in gallery. 
 
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.a1); // Picture to compare. 
 
    // Code to compare picture. 
 

 
    ImageView imageView1; 
 

 
    public void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 

 
     imageView1 = (ImageView)findViewById(R.id.imageView); 
 

 
     Button buttonIntent = (Button)findViewById(R.id.buttonIntent); 
 
     buttonIntent.setOnClickListener(new View.OnClickListener() { 
 
      public void onClick(View v) { 
 
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
 
       intent.setType("image/*"); 
 
       startActivityForResult(Intent.createChooser(intent 
 
         , "Select Picture"), REQUEST_GALLERY); 
 
      } 
 
     }); 
 

 
     Button buttonIntent2 = (Button)findViewById(R.id.button); 
 
     buttonIntent2.setOnClickListener(new View.OnClickListener() { 
 
      public void onClick(View v) { 
 
       if(bitmap == b) { // Have to error. 
 
        Intent newActivity = new Intent(MainActivity.this, ActivityForm2.class); 
 
        startActivity(newActivity); 
 
       } 
 
      } 
 
     }); 
 

 
    } 
 

 
    public void onActivityResult(int requestCode, int resultCode 
 
      , Intent data) { 
 
     if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) { 
 
      Uri uri = data.getData(); 
 
      try { 
 
       bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 
 
       imageView1.setImageBitmap(bitmap); 
 
      } catch (FileNotFoundException e) { 
 
       e.printStackTrace(); 
 
      } catch (IOException e) { 
 
       e.printStackTrace(); 
 
      } 
 
     } 
 
    } 
 
}

  1. ActivityForm2.java

  1. Bildbeispiel

5,1 Bild Bitmap Bitmap;

enter image description here

5,2 Image to Bitmap b = BitmapFactory.decodeResource (GetResources(), R.drawable.a1);

enter image description here

  1. Runtime Error

11-13 21:08:42.244 26248-26248/com.spv.babaimile.fruitsearchwithpic E/AndroidRuntime: FATAL EXCEPTION: main 
 
                        Process: com.spv.babaimile.fruitsearchwithpic, PID: 26248 
 
                        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.spv.babaimile.fruitsearchwithpic/com.spv.babaimile.fruitsearchwithpic.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548) 
 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
 
                         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
 
                         at android.os.Looper.loop(Looper.java:154) 
 
                         at android.app.ActivityThread.main(ActivityThread.java:6077) 
 
                         at java.lang.reflect.Method.invoke(Native Method) 
 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
 
                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
 
                         at android.content.ContextWrapper.getResources(ContextWrapper.java:86) 
 
                         at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127) 
 
                         at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121) 
 
                         at com.spv.babaimile.fruitsearchwithpic.MainActivity.<init>(MainActivity.java:22) 
 
                         at java.lang.Class.newInstance(Native Method) 
 
                         at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) 
 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
 
                         at android.app.ActivityThread.-wrap12(ActivityThread.java)  
 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
 
                         at android.os.Handler.dispatchMessage(Handler.java:102)  
 
                         at android.os.Looper.loop(Looper.java:154)  
 
                         at android.app.ActivityThread.main(ActivityThread.java:6077)  
 
                         at java.lang.reflect.Method.invoke(Native Method)  
 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Antwort

Verwandte Themen