Android: BitmapFactory.decodeFile / OpenCV does return invaild Bitmap

寵の児 提交于 2019-12-11 08:19:12

问题


I'm taking a picture with the camera activity and then try to read it from the saved location. Unfortunately the Bitmap returnd by:

Bitmap bimage = BitmapFactory.decodeFile( path );

has height and width = -1. I'm using OpenCV and when I read the image with

Mat img = Highgui.imread(path);

I get a proper matrix. Altough I cannot convert it to a bitmap. When doing

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

I get the same result again: A Bitmap with width and height = -1. Do I need to watch out for the image format? Can I set that on the camera intent? Shouldn't

BitmapFactory.decodeFile( path );

automatically realize what the format is?

I'm running this on a Samsung galaxy S with Android 2.3.1 and OpenCV 2.3.1

Thanks for your help.


回答1:


Samsung has some issues with camera intent Photo capture Intent causes NullPointerException on Samsung phones only

Try this way

// to call camera

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

// in activity 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);
            }

        }


来源:https://stackoverflow.com/questions/9044909/android-bitmapfactory-decodefile-opencv-does-return-invaild-bitmap

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