Android比较2张图片并告诉水果类别是水果(苹果/香蕉)还是水果

时间:2016-11-13 14:07:43

标签: android image image-processing bitmap image-comparison

Android比较2个图像使用位图代码并告诉水果类别是水果(苹果/香蕉)或不是水果。

我有问题与Bitmap和BitmapFactory比较以产生运行时错误,我有问题的解决方案。

  1. activity_main.xml中
  2. 
    
        <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>
    &#13;
    &#13;
    &#13;

    1. activity_main2.xml
    2. &#13;
      &#13;
      <?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>
      &#13;
      &#13;
      &#13;

      1. MainActivity.java
      2. &#13;
        &#13;
        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();
                    }
                }
            }
        }
        &#13;
        &#13;
        &#13;

        1. ActivityForm2.java
        2. &#13;
          &#13;
          import android.os.Bundle;
          import android.support.v7.app.AppCompatActivity;
          
          /**
           * Created by sumate on 10/30/2016 AD.
           */
          public class ActivityForm2 extends AppCompatActivity {
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main2);
              }
          
          }
          &#13;
          &#13;
          &#13;

          1. 图片示例
          2. 5.1图像到位图位图;

            enter image description here

            5.2图像到位图b = BitmapFactory.decodeResource(getResources(),R.drawable.a1);

            enter image description here

            1. 运行时错误
            2. &#13;
              &#13;
              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) 
              &#13;
              &#13;
              &#13;

1 个答案:

答案 0 :(得分:0)