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?

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
zakjma
  • 2,030
  • 12
  • 40
  • 81
  • 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? – greenapps Mar 24 '15 at 12:13
  • I don't understand that how I can use getResources().openRawResource(R.drawable.icon) with Highgui.imread function. – zakjma Mar 24 '15 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. – greenapps Mar 24 '15 at 12:57
  • Highgui loads image using only `imread()` function. There is no another function for this. – zakjma Mar 24 '15 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. – greenapps Mar 24 '15 at 13:05
  • Probably I should use `openRawResource` via `InputStream inpT = getResources().openRawResource(R.drawable.img);` – zakjma Mar 24 '15 at 16:31

3 Answers3

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);
Tima
  • 12,765
  • 23
  • 82
  • 125
Zain ul abdeen
  • 155
  • 1
  • 5
  • 15
  • @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 Mar 29 '17 at 09:02
  • @muetzenflo I fixed the call. Hopefully Zain won't be angry because of that – Tima Apr 04 '17 at 11:41
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;
}
zakjma
  • 2,030
  • 12
  • 40
  • 81
0

should be doing instead is:

  Mat m = Highgui.imread(file.getAbsolutePath());
Jai
  • 486
  • 1
  • 8
  • 21
  • 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!"); }' – zakjma Mar 24 '15 at 12:55