0

我有一个正在播放视频的纹理视图。我需要对播放视频的帧进行截图。早些时候我正在使用一种方法

public Bitmap getFrameAsBitmap() {
        Bitmap bmp = textureview.getBitmap();
        Canvas canvas = new Canvas(bmp);
        textureview.draw(canvas);
        return bmp;
    }

它在大多数设备上运行良好,但在三星 M 系列上我无法截屏。只有黑屏来了。然后我尝试了

public Bitmap getFrameAsBitmap() {
        View view = textureview;
        view.setDrawingCacheEnabled(true);
        Bitmap bmp = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        textureview.draw(canvas);
        return bmp;
    }

但是这种方法不会在任何手机上返回数据。有什么想法该怎么做?

4

1 回答 1

0

您可以尝试以下几件事。

直接返回位图。

public Bitmap getFrameAsBitmap() {
    return textureview.getBitmap();
}

克隆位图,然后返回克隆。

public Bitmap getFrameAsBitmap() {
    Bitmap bmp = textureview.getBitmap();
    Bitmap clone = bmp.copy(Bitmap.Config.ARGB_8888, true);
    return clone;
}

在新画布上绘制位图。

public Bitmap getFrameAsBitmap() {
    Bitmap bmp = textureview.getBitmap();
    //Try using bitmap's width/height first, if it does not work, use view's width/height instead.
    Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
    //Bitmap newBmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBmp);
    //It is not efficient to create a Paint object too often. Better make it as global variable.
    canvas.drawBitmap(bmp, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));
    return newBmp;
}
于 2021-06-22T09:15:05.290 回答