0

我正在尝试将 InputSteam(视频)转换为位图,但是 decodeStream() 返回 null。

代码示例:

InputStream is = getResources().openRawResource(R.drawable.test1);
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap surface = BitmapFactory.decodeStream(is,null,options);
//surface is null

是不是因为输入的蒸汽太大了?如果是这样,我将如何修剪输入流以仅读取 1 1920x1080 帧?这需要非常快,我尝试使用 MediaMetadataRetriever 但它太慢了。大局是我正在尝试将 .mp4 绘制到画布上。

4

1 回答 1

0

尝试这个

Bitmap surface = decodeFile(new File("file path here"));

    private Bitmap decodeFile(File f){

      int IMGAE_REZ  = 100;

        try {

            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){

                if(width_tmp/2<IMGAE_REZ || height_tmp/2<IMGAE_REZ)
                break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }
            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}

        return null;
    }   
于 2013-11-05T00:20:07.640 回答