gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:53:25 30 4
gpt4 key购买 nike

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

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

  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

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);
}

}

  1. 图片示例

5.1 Image to Bitmap位图;

enter image description here

5.2 图片转Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.a1);

enter image description here

  1. 运行时错误

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) 

最佳答案

我已经回答将水果与针对 Android 的 OpenCV 颜色 Blob 检测进行比较。

来源:https://github.com/doanga2007/FruitSearch

教程:https://docs.google.com/presentation/d/1s0KvqZm0kmPYRdjQNgKFPan0RjNWBCjkem23UkXh2GE/edit#slide=id.g2e200109_1_0

关于Android比较2张图片并告诉水果类别是水果(苹果/香蕉)还是不是水果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574706/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com