2

I tried reading an image from drawable folder. I am using eclipse ide. I ran the below code but the image was not loaded. The code is taken from here

Mat image =  new Mat(new Size(500,500 ),CvType.CV_8U);// Change CvType as you need.
            image = Highgui.imread("icon.png");
            if(image.empty()) {
                Log.i(TAG, "Empty image!");
            }

My Screenshot of my drawable folder is below : enter image description here

How can I load this image?

6
  • Please tell if Highgui has a method to read from an inputstream. If so, you could open a stream like this: getResources().openRawResource(R.drawable.icon). Or maybe Highgui can read from resources? Commented Mar 24, 2015 at 12:13
  • I don't understand that how I can use getResources().openRawResource(R.drawable.icon) with Highgui.imread function. Commented Mar 24, 2015 at 12:52
  • Not with imread() of course. That's why i asked you Please tell if Highgui has a method to read from an inputstream.. So why didn't you answer my question? You can easily get a list of all available functions for Highgui in the IDE. Commented Mar 24, 2015 at 12:57
  • Highgui loads image using only imread() function. There is no another function for this. Commented Mar 24, 2015 at 13:04
  • Well than you have to copy your icon from the drawable resource to the file system as file. Then use that file. Commented Mar 24, 2015 at 13:05

3 Answers 3

6

You can just simply do this

Mat m = Utils.loadResource(MainActivity.this, R.drawable.img, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, bm);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);
Sign up to request clarification or add additional context in comments.

2 Comments

@zumma this should be the correct answer. But: Highgui is not available on Android. It's been split into separate packages ("videoio" and "imgcodecs"). But you can also use Utils.loadResource(Context, int)
@muetzenflo I fixed the call. Hopefully Zain won't be angry because of that
1

I found an example for this work.

InputStream inpT = getResources().openRawResource(R.drawable.imgt);
mTemp = readInputStreamIntoMat(inpT);

private static Mat readInputStreamIntoMat(InputStream inputStream) throws IOException {
    // Read into byte-array
    byte[] temporaryImageInMemory = readStream(inputStream);

    // Decode into mat. Use any IMREAD_ option that describes your image appropriately
    Mat outputImage = Highgui.imdecode(new MatOfByte(temporaryImageInMemory), Highgui.IMREAD_GRAYSCALE);

    return outputImage;
}

private static byte[] readStream(InputStream stream) throws IOException {
    // Copy content of the image to byte-array
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = stream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();
    byte[] temporaryImageInMemory = buffer.toByteArray();
    buffer.close();
    stream.close();
    return temporaryImageInMemory;
}

Comments

0

should be doing instead is:

  Mat m = Highgui.imread(file.getAbsolutePath());

1 Comment

I tried it, but it cannot loaded. My code is : 'Mat image = new Mat(new Size(500,500 ),CvType.CV_8U);// Change CvType as you need. File file = new File("icon.png"); image = Highgui.imread(file.getAbsolutePath()); if(image.empty()) { Log.i(TAG, "Empty image!"); }'

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.