在Android Studio中从数据库发出比较图像

ddrv8njm  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(96)

我有图像视图,按钮和文本在我的活动,我想要以下的东西:首先,当我点击按钮,它会打开画廊,当我从画廊中选择图像,它显示在我的图像视图,其次,当图像显示它会比较从我的数据库中已经保存在那里的图像,如果图像匹配,然后显示图像发现,并显示用户数据,如年龄联系人和姓名在我的textview或如果图像不相同,然后显示图像找不到。用于比较。我该怎么解决呢????
我知道,我们需要一个比较算法,但在这里我只是做一个简单的方法,通过比较tha上传的URI和数据库图像的URL,但在我的应用程序中上传图像后,它显示图像没有找到我尝试通过检查Catlog也调试模式,但没有找到问题的每一种方法。
以下是我的活动:

package com.example.abnormal_person_app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class Fetch_image extends AppCompatActivity {
    private ImageView imageView;
    private Button uploadImageButton;
    private TextView userInfoTextView;
    private Uri selectedImageUri;

    private static final int PICK_IMAGE_REQUEST_CODE = 1;
    private static final String DATABASE_REFERENCE = "users"; // Adjust this to your Firebase Realtime Database reference

    public void onBackPressed() {
        // Disable the back button by not calling super.onBackPressed()
        // You can add any custom logic here if needed
        // For example, show a message to the user or do nothing
        // To re-enable the back button, simply remove this method override

        Intent intent=new Intent(getApplicationContext(),Dashboard.class);
        startActivity(intent);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fetch_image);

        FirebaseApp.initializeApp(this);

        imageView = findViewById(R.id.imageView);
        uploadImageButton = findViewById(R.id.uploadImageButton);
        userInfoTextView = findViewById(R.id.userInfoTextView);

        uploadImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery();
            }
        });
    }

    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
            selectedImageUri = data.getData();
            imageView.setImageURI(selectedImageUri);

            Log.d("SelectedImageURI", selectedImageUri.toString());

            // You have the selected image URI, now proceed with image comparison
            compareImageWithDatabase();
        }
    }

    private void compareImageWithDatabase() {
        // Convert the selected image URI to a string (e.g., Firebase Storage reference or download URL)
        String selectedImagePath = selectedImageUri.toString();

        Log.d("SelectedImagePath", selectedImagePath);

        // Initialize Firebase Database reference
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference databaseReference = firebaseDatabase.getReference(DATABASE_REFERENCE);

        // Query the Firebase Realtime Database to find a user with a matching image path
        Query query = databaseReference.orderByChild("imageUrl").equalTo(selectedImagePath);

        query.get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                DataSnapshot dataSnapshot = task.getResult();
                if (dataSnapshot.exists()) {
                    // Match found - retrieve user data
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        User user = snapshot.getValue(User.class);
                        if (user != null) {
                            displayUserData(user.getName(), user.getAge(), user.getContact());
                            return;
                        }
                    }
                }
            }

            // No match found
            Log.d("ImageComparison", "No match found");
            displayNotFound();
        });
    }

    private void displayUserData(String name, int age, String contact) {
        String userData = "Name: " + name + "\nAge: " + age + "\nContact: " + contact;
        userInfoTextView.setText(userData);

        Log.d("DisplayUserData", "User data displayed: " + userData);
    }

    private void displayNotFound() {
        userInfoTextView.setText("Image not found");

        Log.d("DisplayNotFound", "Image not found");
    }
}

下面是我的数据库结构:

users
-NgKo_poWoq4PygtChdk
age
:
12

contact
:
"03120767949"
imageUrl
:
"https://firebasestorage.googleapis.com/v0/b/abnormal-person-app.appspot.com/o/profile_images%2Fnull_1696878188592.jpg?alt=media&token=6fbed5a0-dba8-46e5-8801-b68ead8f1edf"
name
:
"iftikhat"
-NgQowbH84HGMWLxXmTd
age
:
25
contact
:
"213487878"
imageUrl
:
"https://firebasestorage.googleapis.com/v0/b/abnormal-person-app.appspot.com/o/profile_images%2Fnull_1696978942978.jpg?alt=media&token=4a515511-0d64-4d9e-a80a-6d5b5a98034e"
name
:
"kahan"
-NgR1PG5xirNZruKiXW9
age
:
25
contact
:
"87876784"
imageUrl
:
Value
https://firebasestorage.googleapis.com/v0/b/abnormal-person-app.appspot.com/o/profile_images%2Fnull_1696982473830.jpg?alt=media&token=c54b0531-34a8-4241-bdb1-d62542cb026e
name
:
"apcho"

下面是XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_klia"
    tools:context=".Fetch_image">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        tools:ignore="UselessParent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="150dp"
            android:layout_height="200dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/placeholder_image"
            android:visibility="visible"/>

        <Button
            android:id="@+id/uploadImageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Upload Image"
            android:layout_marginTop="20dp"/>

        <TextView
            android:id="@+id/userInfoTextView"
            android:layout_width="wrap_content"
            android:layout_height="240dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:clickable="true"
            android:hint="User's Info will Appear here if Match Found....."
            android:text=""
            android:textAlignment="center"
            android:textSize="16sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/whatsapp_imgview"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/whatapp_pic"
            android:visibility="gone"/>

    </LinearLayout>
</LinearLayout>
iaqfqrcu

iaqfqrcu1#

如何将您的图像上传到存储?在您的数据库中,您存储的是firebase存储的路径,其中您的图像存储在服务器上,您的本地路径/uri是不同的,您应该在数据库中添加新的字段,如localPath,将存储本地路径/uri,然后您可以使用localPath获取数据

相关问题