1. 程式人生 > 程式設計 >Android使用Opengl錄影時新增水印

Android使用Opengl錄影時新增水印

最近需要開發一個類似行車記錄儀的app,其中需要給錄製的視訊新增動態水印。我使用的是OpenGL開發的,剛開始實現的是靜態水印,後面才實現的動態水印。

先上效果圖,左下角的是靜態水印,中間偏下的是時間水印(動態水印):

Android使用Opengl錄影時新增水印

一、靜態水印

實現原理:錄影時是通過OpenGL把影象渲染到GLSurfaceView上的,通俗的講,就是把圖片畫到一塊畫布上,然後展示出來。新增圖片水印,就是把水印圖片跟錄製的影象一起畫到畫布上。

這是載入紋理跟陰影的Java類

package com.audiovideo.camera.blog;


import android.opengl.GLES20;


/**
 * Created by fenghaitao on 2019/9/12.
 */

public class WaterSignSProgram{

  private static int programId;
  private static final String VERTEX_SHADER =
          "uniform mat4 uMVPMatrix;\n" +
          "attribute vec4 aPosition;\n" +
          "attribute vec4 aTextureCoord;\n" +
          "varying vec2 vTextureCoord;\n" +
          "void main() {\n" +
          "  gl_Position = uMVPMatrix * aPosition;\n" +
          "  vTextureCoord = aTextureCoord.xy;\n" +
          "}\n";

  private static final String FRAGMENT_SHADER =
          "precision mediump float;\n" +
          "varying vec2 vTextureCoord;\n" +
          "uniform sampler2D sTexture;\n" +
          "void main() {\n" +
          "  gl_FragColor = texture2D(sTexture,vTextureCoord);\n" +
          "}\n";

  public WaterSignSProgram() {
    programId = loadShader(VERTEX_SHADER,FRAGMENT_SHADER);

    uMVPMatrixLoc = GLES20.glGetUniformLocation(programId,"uMVPMatrix");
    checkLocation(uMVPMatrixLoc,"uMVPMatrix");
    aPositionLoc = GLES20.glGetAttribLocation(programId,"aPosition");
    checkLocation(aPositionLoc,"aPosition");
    aTextureCoordLoc = GLES20.glGetAttribLocation(programId,"aTextureCoord");
    checkLocation(aTextureCoordLoc,"aTextureCoord");
    sTextureLoc = GLES20.glGetUniformLocation(programId,"sTexture");
    checkLocation(sTextureLoc,"sTexture");
  }

  public int uMVPMatrixLoc;
  public int aPositionLoc;
  public int aTextureCoordLoc;
  public int sTextureLoc;

  public static void checkLocation(int location,String label) {
    if (location < 0) {
      throw new RuntimeException("Unable to locate '" + label + "' in program");
    }
  }

/**
 * 載入編譯連線陰影
 * @param vss source of vertex shader
 * @param fss source of fragment shader
 * @return
 */
public static int loadShader(final String vss,final String fss) {
  Log.v(TAG,"loadShader:");
  int vs = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
  GLES20.glShaderSource(vs,vss);
  GLES20.glCompileShader(vs);
  final int[] compiled = new int[1];
  GLES20.glGetShaderiv(vs,GLES20.GL_COMPILE_STATUS,compiled,0);
  if (compiled[0] == 0) {
   Log.e(TAG,"Failed to compile vertex shader:"
      + GLES20.glGetShaderInfoLog(vs));
   GLES20.glDeleteShader(vs);
   vs = 0;
  }

  int fs = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
  GLES20.glShaderSource(fs,fss);
  GLES20.glCompileShader(fs);
  GLES20.glGetShaderiv(fs,0);
  if (compiled[0] == 0) {
   Log.w(TAG,"Failed to compile fragment shader:"
     + GLES20.glGetShaderInfoLog(fs));
   GLES20.glDeleteShader(fs);
   fs = 0;
  }

  final int program = GLES20.glCreateProgram();
  GLES20.glAttachShader(program,vs);
  GLES20.glAttachShader(program,fs);
  GLES20.glLinkProgram(program);

  return program;
}

  /**
   * terminatinng,this should be called in GL context
   */
  public static void release() {
    if (programId >= 0)
      GLES20.glDeleteProgram(programId);
    programId = -1;
  }
}
package com.audiovideo.camera.blog;

import android.opengl.GLES20;
import android.opengl.Matrix;

import com.audiovideo.camera.glutils.GLDrawer2D;
import com.audiovideo.camera.utils.LogUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;


這是畫水印的Java類
/**
 * Created by fenghaitao on 2019/9/12.
 */

public class WaterSignature {

  private static final String VERTEX_SHADER =
      "uniform mat4 uMVPMatrix;\n" +
          "attribute vec4 aPosition;\n" +
          "attribute vec4 aTextureCoord;\n" +
          "varying vec2 vTextureCoord;\n" +
          "void main() {\n" +
          "  gl_Position = uMVPMatrix * aPosition;\n" +
          "  vTextureCoord = aTextureCoord.xy;\n" +
          "}\n";

  private static final String FRAGMENT_SHADER =
      "precision mediump float;\n" +
          "varying vec2 vTextureCoord;\n" +
          "uniform sampler2D sTexture;\n" +
          "void main() {\n" +
          "  gl_FragColor = texture2D(sTexture,vTextureCoord);\n" +
          "}\n";

  public static final int SIZE_OF_FLOAT = 4;
  /**
   * 一個“完整”的正方形,從兩維延伸到-1到1。
   * 當 模型/檢視/投影矩陣是都為單位矩陣的時候,這將完全覆蓋視口。
   * 紋理座標相對於矩形是y反的。
   * (This seems to work out right with external textures from SurfaceTexture.)
   */
  private static final float FULL_RECTANGLE_COORDS[] = {
      -1.0f,-1.0f,// 0 bottom left
      1.0f,// 1 bottom right
      -1.0f,1.0f,// 2 top left
      1.0f,// 3 top right
  };
  private static final float FULL_RECTANGLE_TEX_COORDS[] = {
      0.0f,//0 bottom left   //0.0f,0.0f,//1 bottom right  //1.0f,// 1 bottom right
      0.0f,//2 top left    //0.0f,//3 top right    //1.0f,// 3 top right
  };

  private FloatBuffer mVertexArray;
  private FloatBuffer mTexCoordArray;
  private int mCoordsPerVertex;
  private int mCoordsPerTexture;
  private int mVertexCount;
  private int mVertexStride;
  private int mTexCoordStride;
  private int hProgram;

  public float[] mProjectionMatrix = new float[16];// 投影矩陣
  public float[] mViewMatrix = new float[16]; // 攝像機位置朝向9引數矩陣
  public float[] mModelMatrix = new float[16];// 模型變換矩陣
  public float[] mMVPMatrix = new float[16];// 獲取具體物體的總變換矩陣
  private float[] getFinalMatrix() {
    Matrix.multiplyMM(mMVPMatrix,mViewMatrix,mModelMatrix,0);
    Matrix.multiplyMM(mMVPMatrix,mProjectionMatrix,mMVPMatrix,0);
    return mMVPMatrix;
  }

  public WaterSignature() {
    mVertexArray = createFloatBuffer(FULL_RECTANGLE_COORDS);
    mTexCoordArray = createFloatBuffer(FULL_RECTANGLE_TEX_COORDS);
    mCoordsPerVertex = 2;
    mCoordsPerTexture = 2;
    mVertexCount = FULL_RECTANGLE_COORDS.length / mCoordsPerVertex; // 4
    mTexCoordStride = 2 * SIZE_OF_FLOAT;
    mVertexStride = 2 * SIZE_OF_FLOAT;

    Matrix.setIdentityM(mProjectionMatrix,0);
    Matrix.setIdentityM(mViewMatrix,0);
    Matrix.setIdentityM(mModelMatrix,0);
    Matrix.setIdentityM(mMVPMatrix,0);
    hProgram = GLDrawer2D.loadShader(VERTEX_SHADER,FRAGMENT_SHADER);
    GLES20.glUseProgram(hProgram);
  }

  private FloatBuffer createFloatBuffer(float[] coords) {
    ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * SIZE_OF_FLOAT);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(coords);
    fb.position(0);
    return fb;
  }

  private WaterSignSProgram mProgram;

  public void setShaderProgram(WaterSignSProgram mProgram) {
    this.mProgram = mProgram;
  }

  public void drawFrame(int mTextureId) {
    GLES20.glUseProgram(hProgram);
    // 設定紋理
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,mTextureId);
    GLES20.glUniform1i(mProgram.sTextureLoc,0);
    GlUtil.checkGlError("GL_TEXTURE_2D sTexture");
    // 設定 model / view / projection 矩陣
    GLES20.glUniformMatrix4fv(mProgram.uMVPMatrixLoc,1,false,getFinalMatrix(),0);
    GlUtil.checkGlError("glUniformMatrix4fv uMVPMatrixLoc");
    // 使用簡單的VAO 設定頂點座標資料
    GLES20.glEnableVertexAttribArray(mProgram.aPositionLoc);
    GLES20.glVertexAttribPointer(mProgram.aPositionLoc,mCoordsPerVertex,GLES20.GL_FLOAT,mVertexStride,mVertexArray);
    GlUtil.checkGlError("VAO aPositionLoc");
    // 使用簡單的VAO 設定紋理座標資料
    GLES20.glEnableVertexAttribArray(mProgram.aTextureCoordLoc);
    GLES20.glVertexAttribPointer(mProgram.aTextureCoordLoc,mCoordsPerTexture,mTexCoordStride,mTexCoordArray);
    GlUtil.checkGlError("VAO aTextureCoordLoc");
    // GL_TRIANGLE_STRIP三角形帶,這就為啥只需要指出4個座標點,就能畫出兩個三角形了。
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP,mVertexCount);
    // Done -- 解綁~
    GLES20.glDisableVertexAttribArray(mProgram.aPositionLoc);
    GLES20.glDisableVertexAttribArray(mProgram.aTextureCoordLoc);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
    GLES20.glUseProgram(0);
  }

  /**
   * terminatinng,this should be called in GL context
   */
  public void release() {
    if (hProgram >= 0)
      GLES20.glDeleteProgram(hProgram);
    hProgram = -1;
  }

  /**
   * 刪除texture
   */
  public static void deleteTex(final int hTex) {
    LogUtil.v("WaterSignature","deleteTex:");
    final int[] tex = new int[] {hTex};
    GLES20.glDeleteTextures(1,tex,0);
  }

}

沒時間了。先寫到這,後面是呼叫,遲點再寫。

下面是如何把水印繪製到畫布上:

1、在SurfaceTexture的onSurfaceCreated方法中初始化並設定陰影;

@Override
   public void onSurfaceCreated(final GL10 unused,final EGLConfig config) {
     LogUtil.v(TAG,"onSurfaceCreated:");
     // This renderer required OES_EGL_image_external extension
     final String extensions = GLES20.glGetString(GLES20.GL_EXTENSIONS);  // API >= 8
   // 使用黃色清除介面
     GLES20.glClearColor(1.0f,1.0f);
     //設定水印
      if (mWaterSign == null) {
        mWaterSign = new WaterSignature();
      }
      //設定陰影
     mWaterSign.setShaderProgram(new WaterSignSProgram());
    mSignTexId = loadTexture(MyApplication.getContext(),R.mipmap.watermark);
   }

這裡是生成mSignTexId 的方法,把該影象與紋理id繫結並返回:

public static int loadTexture(Context context,int resourceId) {
  final int[] textureObjectIds = new int[1];
  GLES20.glGenTextures(1,textureObjectIds,0);
  if(textureObjectIds[0] == 0){
    Log.e(TAG,"Could not generate a new OpenGL texture object!");
    return 0;
  }
  final BitmapFactory.Options options = new BitmapFactory.Options();
  options.inScaled = false;  //指定需要的是原始資料,非壓縮資料
  final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),resourceId,options);
  if(bitmap == null){
    Log.e(TAG,"Resource ID "+resourceId + "could not be decode");
    GLES20.glDeleteTextures(1,0);
    return 0;
  }
  //告訴OpenGL後面紋理呼叫應該是應用於哪個紋理物件
  GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textureObjectIds[0]);
  //設定縮小的時候(GL_TEXTURE_MIN_FILTER)使用mipmap三執行緒過濾
  GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR_MIPMAP_LINEAR);
  //設定放大的時候(GL_TEXTURE_MAG_FILTER)使用雙執行緒過濾
  GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
  //Android裝置y座標是反向的,正常圖顯示到裝置上是水平顛倒的,解決方案就是設定紋理包裝,紋理T座標(y)設定鏡面重複
  //ball讀取紋理的時候 t範圍座標取正常值+1
  //GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_MIRRORED_REPEAT);
  GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,bitmap,0);
  bitmap.recycle();
  //快速生成mipmap貼圖
  GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
  //解除紋理操作的繫結
  GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
  return textureObjectIds[0];
}

2、在繪製方法onDrawFrame中繪製畫面的同時把水印繪製進去;

/**
    * 繪圖到glsurface
    * 我們將rendermode設定為glsurfaceview.rendermode_when_dirty,
    * 僅當呼叫requestrender時呼叫此方法(=需要更新紋理時)
    * 如果不在髒時設定rendermode,則此方法的最大呼叫速度為60fps。
    */
   @Override
   public void onDrawFrame(final GL10 unused) {
     GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
     GLES20.glEnable(GLES20.GL_BLEND);
     //開啟GL的混合模式,即影象疊加
     GLES20.glBlendFunc(GLES20.GL_ONE,GLES20.GL_ONE_MINUS_SRC_ALPHA);
     /**
     *中間這裡是你繪製的預覽畫面
     */
   //畫水印(非動態)
    GLES20.glViewport(20,20,288,120);
   mWaterSign.drawFrame(mSignTexId);
    }

這裡最重要的是要開啟GL的混合模式,即影象疊加,不然你繪製的水印會覆蓋原先的預覽畫面

//開啟GL的混合模式,即影象疊加
GLES20.glBlendFunc(GLES20.GL_ONE,GLES20.GL_ONE_MINUS_SRC_ALPHA);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。