【问题标题】:how to add timestamp like a camcorder while recording video using GLES20如何在使用 GLES20 录制视频时像摄像机一样添加时间戳
【发布时间】:2017-12-02 06:52:43
【问题描述】:

我想在录制视频时显示日期/时间,当我们重播视频时应该显示它,就像我们在闭路电视视频录制中那样。我可以使用 GLES20 显示形状我想在视频中使用文本来显示时间戳我正在使用 textureview 和 Mediarecorder 当我运行 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;
    }

【问题讨论】:

  • 好的,但是您面临的问题是什么?
  • @suvartheec 当我运行 GLText() 时,它什么都不显示,而不是视频上的文本“hellooo”
  • 请通过编辑将信息添加到问题中

标签: android video textureview gles20


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,即无法使用 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 中有一个错误,他们在 createTextureObject() 中使用了 GLES11Ext.GL_TEXTURE_EXTERNAL_OES Texture2dProgram,而不是 mTargetTexture

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多