0

I am trying to read image file using OpenCV Imgcodecs.imread(img) but the image Mat is always empty, I'm using this code:

package com.halocdz.qreagle;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.QRCodeDetector;

public class MainActivity extends AppCompatActivity {

    TextView detectedData_text;
    TextView detectStatus_text;

    static
    {
        if (!OpenCVLoader.initDebug())
            Log.e("OpenCv", "Unable to load OpenCV");
        else
            Log.d("OpenCv", "OpenCV loaded");
    }

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

        // Initialize
        detectStatus_text = (TextView)findViewById(R.id.detectStatus_text);
        detectedData_text = (TextView)findViewById(R.id.detectedData_text);
    }

    @SuppressLint("SetTextI18n")
    public void onClickDetect(View view)
    {
        String path = getResources().getResourceName(R.drawable.qrcode);
        Mat img = Imgcodecs.imread(path + ".png");
        Mat points = new Mat();

        QRCodeDetector qrdetector = new QRCodeDetector();
        detectStatus_text.setText("Detecting QRCode, standby...");
        String data;

        if (img.empty())
        {
            detectStatus_text.setText("Error could not load image file!");
            return;
        }

        if (!qrdetector.detect(img, points))
        {
            detectStatus_text.setText("Error detecting QRCode!");
            return;
        }
        data = qrdetector.decode(img, points);

        if (data.isEmpty())
        {
            detectStatus_text.setText("Error decoding QRCode!");
            return;
        }

        detectedData_text.append(path +"\n\n");
        detectStatus_text.setText("QRCode successfully detected!");
    }
}

It appears that Imgcodecs.imread() not finding the file path to load the file, if so I don't know how to get file correct path in app resources.

Note: I put qrcode.png file in app\src\main\res\drawable\qrcode.png.

1 Answer 1

3

I am not sure how to do with Imgcodecs.imread() in a proper way. However, if you only want to read a Mat object from a drawable resources, I have two workarounds

1) By using bitmap

Bitmap bMap=BitmapFactory.decodeResource(getResources(),R.drawable.qrcode);
Mat img = new Mat();
Utils.bitmapToMat(bMap, img);

where qrcode is a image under Resources folder of your Android project. Then convert Bitmap to bytes or Mat and process in C++ (OpenCV) or Java with matToBitmap or MatToBitmap methods in android-opencv. Ref and Ref2

2) Resource loader from org.opencv.android.utils

Mat img = null;
try {
    img = Utils.loadResource(this, R.drawable.qrcode, CvType.CV_8UC4);
} catch (IOException e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.