Load images with OpenCV from assets folder in Android

我与影子孤独终老i 提交于 2020-01-04 04:11:07

问题


I'm stuck trying to load an image placed in assets folder with OpenCV 3.0 in Android. I've read a lot of answers here, but I can't figure out what I'm doing wrong.

"my image.jpg" is place directly in the assets folder created by Android Studio. This is the code I'm using. I've checked and the library has been loaded correctly.

        Mat imgOr = Imgcodecs.imread("file:///android_asset/myimage.jpg");
        int height = imgOr.height();
        int width = imgOr.width();
        String h = Integer.toString(height);
        String w = Integer.toString(width);

        if (imgOr.dataAddr() == 0) {
            // If dataAddr() is different from zero, the image has been loaded
            // correctly
            Log.d(TAG, "WRONG UPLOAD");
        }

        Log.d(h, "height");
        Log.d(w, "width");

When I try to run my app, this is what I get:

08-21 18:13:32.084 23501-23501/com.example.android D/MyActivity: WRONG UPLOAD
08-21 18:13:32.085 23501-23501/com.example.android D/0: height
08-21 18:13:32.085 23501-23501/com.example.android D/0: width

It seems like the image has no dimensions. I guess because it has not been loaded correctly. I'va also tried to load it placing it in the drawable folder, but it doesn't work anyway and I'd prefer to use the assets one. Anyone can please help me and tell me how to find the right path of the image?

Thanks


回答1:


Problem: imread needs absolute path and your assets are inside a apk, and the underlying c++ classes cannot read from there.

Option 1: load image into Mat without using imread from drawable folder.

                InputStream stream = null;
                Uri uri = Uri.parse("android.resource://com.example.aaaaa.circulos/drawable/bbb_2");
                try {
                    stream = getContentResolver().openInputStream(uri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

                Bitmap bmp = BitmapFactory.decodeStream(stream, null, bmpFactoryOptions);
                Mat ImageMat = new Mat();
                Utils.bitmapToMat(bmp, ImageMat);

Option 2: copy image to cache and load from absolute path.

File file = new File(context.getCacheDir() + "/" + filename);
if (!file.exists())
try {

InputStream is = context.getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();

FileOutputStream fos = new FileOutputStream(file);

fos.write(buffer);
fos.close();
} catch (Exception e) {
throw new RuntimeException(e);
}

if (file.exists()) {
 image = cvLoadImage(file.getAbsolutePath(), type);
}


来源:https://stackoverflow.com/questions/39066334/load-images-with-opencv-from-assets-folder-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!