如何使用GLES20录制视频时像摄像机一样添加时间戳

时间:2017-12-02 06:52:43

标签: android video textureview gles20

我希望在录制视频时显示日期/时间,并且应该在我们重播视频时显示,就像我们在CCTV视频录制中一样。我可以使用GLES20显示形状我想在视频中使用文本来显示Timestamp我正在使用textureview以及Mediarecorder andorid当我运行GLText()时它没有显示任何内容而不是文本" hellooo"在视频上。   这是一个例子:

 private void drawBox() {
        GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
        GLES20.glScissor(0, 0, 100, 100);
        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
    } 

this code displays a box but i want to replace this with text and i am unable to find any solution ..

I tried this method but it didn't work 


  public void GLText() {

//        Bitmap bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_4444);
//        Canvas canvas = new Canvas(bitmap);
//        bitmap.eraseColor(0);
//
//        Paint paint = new Paint();
//        paint.setTextSize(18);
//        paint.setAntiAlias(true);
//        paint.setARGB(0xff, 0xff, 0xff, 0xff);
//        paint.setTextAlign(Paint.Align.LEFT);
//        paint.setTextScaleX(0.5f);
//        canvas.drawText("testGLText", 0.f, 15.f, paint);

        Bitmap bitmap = fromText("hellooo",50);
        GLES20.glEnable(GLES20.GL_SCISSOR_TEST);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glEnable(GLES20.GL_BLEND); // this, and the next line
        GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA); // and this were key! I'm still not completely sure as to what this is doing, but it works!
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, bitmap, 0);


    }

 public Bitmap fromText(String text, int textSize) {
        Paint paint = new Paint();
        paint.setTextSize(textSize);
        paint.setColor(Color.WHITE);
        float baseline = -paint.ascent(); // ascent() is negative
        int width = (int) (paint.measureText(text) + 1.0f);
        int height = (int) (baseline + paint.descent() + 1.0f);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setHasAlpha(true);
        Canvas canvas = new Canvas(bitmap);
        // canvas.drawColor(Color.argb(0, 255, 255, 255));
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        canvas.drawText(text, 0, baseline, paint);
        return bitmap;
    }

My entire code is :

 if (showBox && (++mFrameCount & 0x04) == 0) {
            drawBox(); // here drawBox draws a box but i when i call GLText() it draws nothing
        }
    }

    /**
     * Draws a red box in the corner.
     */
    private void drawBox() {
        GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
        GLES20.glScissor(0, 0, 100, 100);
        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
    }

    private void drawSquare() {
        GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
        GLES20.glScissor(200, 300, 900, 100);
        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
    }

    public void GLText() {

//        Bitmap bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_4444);
//        Canvas canvas = new Canvas(bitmap);
//        bitmap.eraseColor(0);
//
//        Paint paint = new Paint();
//        paint.setTextSize(18);
//        paint.setAntiAlias(true);
//        paint.setARGB(0xff, 0xff, 0xff, 0xff);
//        paint.setTextAlign(Paint.Align.LEFT);
//        paint.setTextScaleX(0.5f);
//        canvas.drawText("testGLText", 0.f, 15.f, paint);

        Bitmap bitmap = fromText("hellooo",50);
        GLES20.glEnable(GLES20.GL_SCISSOR_TEST);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glEnable(GLES20.GL_BLEND); // this, and the next line
        GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA); // and this were key! I'm still not completely sure as to what this is doing, but it works!
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, bitmap, 0);


    }


    public Bitmap fromText(String text, int textSize) {
        Paint paint = new Paint();
        paint.setTextSize(textSize);
        paint.setColor(Color.WHITE);
        float baseline = -paint.ascent(); // ascent() is negative
        int width = (int) (paint.measureText(text) + 1.0f);
        int height = (int) (baseline + paint.descent() + 1.0f);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setHasAlpha(true);
        Canvas canvas = new Canvas(bitmap);
        // canvas.drawColor(Color.argb(0, 255, 255, 255));
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        canvas.drawText(text, 0, baseline, paint);
        return bitmap;
    }

2 个答案:

答案 0 :(得分:1)

在这里,我得到了一个开源库,其工作方式类似。我可以在视频中插入文本,还可以做很多其他事情。

https://github.com/INDExOS/media-for-mobile

答案 1 :(得分:0)

我使用Grafika面临同样的问题,即无法在视频纹理上绘制透明文本纹理。我使用以下方法创建了一个透明的文本位图:

public Bitmap fromText(String text, int textSize, @ColorInt int textColor) {
Paint paint = new Paint();
paint.setTextSize(textSize);
paint.setColor(Color.WHITE);
float baseline = -paint.ascent(); // ascent() is negative
int width = (int) (paint.measureText(text) + 1.0f);
int height = (int) (baseline + paint.descent() + 1.0f);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(true);
Canvas canvas = new Canvas(bitmap);
// canvas.drawColor(Color.argb(0, 255, 255, 255));
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawText(text, 0, baseline, paint);
return bitmap;
}

当我将上述位图保存为PNG文件时,它只有白色文本透明,但在使用Grafika渲染时会显示黑色背景。

要将位图设置为纹理句柄,请使用以下命令:

public void setBitmap(Bitmap bitmap, int textureId) {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(mTextureTarget, textureId);
GLES20.glEnable(GLES20.GL_BLEND); // this, and the next line
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA); // and this were key! I'm still not completely sure as to what this is doing, but it works!
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, bitmap, 0);
}

最后,Grafika中有一个错误,他们在GLES11Ext.GL_TEXTURE_EXTERNAL_OES createTextureObject()中使用了Texture2dProgram,而不是mTargetTexture

相关问题