【问题标题】:Android: BitmapFactory.decodeFile / OpenCV does return invaild BitmapAndroid:BitmapFactory.decodeFile / OpenCV 确实返回无效位图
【发布时间】:2012-01-28 11:29:12
【问题描述】:

我正在使用相机活动拍照,然后尝试从保存的位置读取它。不幸的是,返回的位图:

Bitmap bimage = BitmapFactory.decodeFile( path );

高度和宽度 = -1。 我正在使用 OpenCV,当我阅读图像时使用

Mat img = Highgui.imread(path);

我得到了一个合适的矩阵。尽管我无法将其转换为位图。做的时候

bmp = Bitmap.createBitmap(img.cols(), img.rows(), Config.ARGB_8888);
Utils.matToBitmap(img, bmp);

我再次得到相同的结果:宽度和高度 = -1 的位图。我需要注意图像格式吗?我可以在相机意图上设置它吗?不应该

BitmapFactory.decodeFile( path );

自动识别格式是什么?

我在装有 Android 2.3.1 和 OpenCV 2.3.1 的三星 Galaxy S 上运行它

感谢您的帮助。

【问题讨论】:

标签: android opencv bitmapfactory


【解决方案1】:

三星在相机意图方面存在一些问题Photo capture Intent causes NullPointerException on Samsung phones only

试试这个方法

//调用相机

String _path = Environment.getExternalStorageDirectory()
                    + File.separator + "TakenFromCamera.png";
            File file = new File(_path);
            Uri outputFileUri = Uri.fromFile(file);
            Intent intent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, 1212);

//在活动onResult中

if (requestCode == 1212) {
            String _path = Environment.getExternalStorageDirectory()
                    + File.separator + "TakenFromCamera.png";
            mBitmap = BitmapFactory.decodeFile(_path);
            if (mBitmap == null) {
            } else {
                Intent intent = new Intent(AYGCamActivity.this,
                        myGalleryImage.class);

                startActivity(intent);
            }

        }

【讨论】:

  • 谢谢。那行得通:)。我几乎是这样做的,只是我使用了结尾 .jpg。您是否认为这可能会导致问题,因为解码器尝试解码实际为 PNG 的 JPEG...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 2021-01-19
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
相关资源
最近更新 更多