[JAVA] Import device images with Android app

Introduction

I'm tired of making an app that is completed only by the default of Android Studio, so I will expand the range of apps by making it possible to edit various images using OpenCV. First capture an image for editing from your device.

environment

OpenCV 4.1.2 AndroidStudio 3.4.1

Capture images

Click here for Official Documents

First, write the base code appropriately. I'm still not used to making layout screens with GUI, so I write in xml.

MainActivity.java


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    
    Button btn_import;
    ImageView img_picture;

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

        btn_import = findViewById(R.id.btn_import);
        img_picture = findViewById(R.id.img_picture);
    }

    @Override
    protected void onStart() {
        super.onStart();
        btn_import.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Write the process to capture the image here
            }
        });
    }
}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_import"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Import"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="40dp"
        android:layout_marginStart="40dp"/>

    <ImageView
        android:id="@+id/img_picture"
        android:layout_width="320dp"
        android:layout_height="500dp"
        app:layout_constraintTop_toBottomOf="@id/btn_import"
        app:layout_constraintStart_toStartOf="@id/btn_import"/>

</android.support.constraint.ConstraintLayout>

As a method of importing, it is OK if you use either of the following two intents.

~~ It was written that CREATE is recommended when making photo editing apps, so use ʻACTION_CREATE_DOCUMENT ~~ ʻACTION_OPEN_DOCUMENT was more suitable if you just edited it on the spot without saving it.

Implement import processing

Excerpt only for the OnClick part

MainActivity.java


public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    startActivityForResult(intent, READ_REQUEST_CODE);  //READ_REQUSET_CODE = 42
}

Now when you press the button, the picker will pop up and you can select an image. When you select an image, ʻonActivityResult () is called. You can get the URI to refer to the image in this ʻonActivityResult (). This time, get the bitmap from the URI and display the image.

MainActivity.java


@Override
public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {
    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();//Image data uri
            try {
                Bitmap bmp = getBitmapFromUri(uri);
                img_picture.setImageBitmap(bmp);
            }catch(IOException e){
                //TODO:Exception handling
            }
        }
    }
}

//Diverted from the official Doc as it is
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor =
            getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return image;
}

end

Now you can press the button, select an image from the main unit, and display it. Next, I would like to play around with this captured image with OpenCV.

Recommended Posts

Import device images with Android app
Vibrate the wristband device with Bluetooth from the Android app
USB serial communication with Android app (usb-serial-for-android)
Face recognition app with OpenCV + Android Studio
I made a rock-paper-scissors app with android
Android weather app
[Android] Despaired story with App UserId and SharedUserId
Compatible with Android 10 (API 29)
Import JSON with SolrJ
CSV import with BOM
Android app Spectrum Analyzer
[Android] Uploading images from your device to the server
Import documents with SolrJ
About Android App Components
Android app to select and display images from the gallery
How to make an app using Tensorflow with Android Studio
Android app personal development kickoff
ROS app development on Android
Android app decompile ⇒ fix ⇒ recompile
Make an android app. (Day 5)
Is it an Android app?
The basics of the process of making a call with an Android app