使用OpenCV从Android中的Assets文件夹加载图像

使用OpenCV从Android中的Assets文件夹加载图像,第1张

概述我在尝试使用Android中的OpenCV3.0加载放置在资产文件夹中的图像时遇到了麻烦.我在这里阅读了很多答案,但是我无法弄清楚自己在做什么错.“myimage.jpg”直接放置在AndroidStudio创建的资产文件夹中.这是我正在使用的代码.我已经检查过,并且库已正确加载.MatimgOr

我在尝试使用Android中的OpenCV 3.0加载放置在资产文件夹中的图像时遇到了麻烦.我在这里阅读了很多答案,但是我无法弄清楚自己在做什么错.

“ my image.jpg”直接放置在AndroID Studio创建的资产文件夹中.
这是我正在使用的代码.我已经检查过,并且库已正确加载.

        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");

当我尝试运行我的应用程序时,这是我得到的:

08-21 18:13:32.084 23501-23501/com.example.androID D/MyActivity: WRONG UPLOAD08-21 18:13:32.085 23501-23501/com.example.androID D/0: height08-21 18:13:32.085 23501-23501/com.example.androID D/0: wIDth

图像似乎没有尺寸.我猜是因为它没有正确加载.我还尝试加载它,将其放置在drawable文件夹中,但是无论如何它都无法正常工作,我更喜欢使用资产之一.
任何人都可以帮助我,并告诉我如何找到正确的图像路径?

谢谢

解决方法:

问题:imread需要绝对路径,并且您的资产位于apk内,并且基础c类无法从此处读取.

选项1:将图像加载到Mat中,而不使用可绘制文件夹中的读取.

                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);

选项2:将图片复制到缓存并从绝对路径加载.

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);}
总结

以上是内存溢出为你收集整理的使用OpenCV从Android中的Assets文件夹加载图像全部内容,希望文章能够帮你解决使用OpenCV从Android中的Assets文件夹加载图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1120807.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存