2017-12-16 1 views
-1

Android에서 맞춤 워터 마크 렌더러를 만들고 싶습니다.어떻게 워터 마크 렌더러를 만들 수 있습니까?

현재 카메라 미리보기 화면의 오른쪽 하단 모서리에 워터 마크를 추가 할 예정입니다.

내가 작성한 소스 코드는 다음과 같습니다. 이 코드는 작동하지 않습니다.

도와주세요. 고맙습니다.

함수 호출 순서는 다음과 같습니다

-setWatermarkTexture

-createBuffers

-configureOpenGL

-draw

public class WatermarkRenderer implements Renderer { 

    private int[] textureHandles = new int[1]; 

    float vertices[] = { 
      -1, 0.5f, 
      -0.5f, -0.5f, 
      -1, 1, 
      -0.5f, 1, 

    }; 

    float texCoords[] = { 
      0, 0, 
      1, 0, 
      0, 1, 
      1, 1, 
    }; 

    private Bitmap  watermark; 
    private int[] buffers = new int[2]; 
    private float[] projectionMatrix = new float[16]; 


    private FloatBuffer vertexBuffer; 
    private FloatBuffer textureBuffer; 

    @Override 
    public void configureOpenGL() { 

     GLES20.glGenTextures(textureHandles.length, textureHandles, 0); 

     GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandles[0]); 
     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); 
     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); 
     GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); 
     GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); 

     GLES20.glGenBuffers(buffers.length, buffers, 0); 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]); 
     GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, 8, vertexBuffer, GLES20.GL_STATIC_DRAW); 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]); 
     GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, 8, textureBuffer, GLES20.GL_STATIC_DRAW); 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 
     GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 
    } 

    @Override 
    public void createBuffers() { 
     vertexBuffer = FloatBuffer.allocate(8); 
     vertexBuffer.put(vertices); 
     vertexBuffer.position(0); 

     textureBuffer = FloatBuffer.allocate(8); 
     textureBuffer.put(texCoords); 
     textureBuffer.position(0); 
    } 

    @Override 
    public void draw() { 

     GLES20.glEnable(GLES20.GL_DEPTH_TEST); 

     GLES20.glEnable(GLES20.GL_BLEND); 
     //GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA); 
     //GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 

     if (watermark != null) { 
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, watermark, 0); 
      watermark.recycle(); 
      watermark = null; 
     } 
/* 
     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]); 
     GLES20.glEnableVertexAttribArray(AttributeLocations.POSITION); 
     GLES20.glVertexAttribPointer(AttributeLocations.POSITION, 2, GLES20.GL_FLOAT, false, 0, 0); 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]); 
     GLES20.glEnableVertexAttribArray(AttributeLocations.TEXTURE_COORDINATES); 
     GLES20.glVertexAttribPointer(AttributeLocations.TEXTURE_COORDINATES, 2, GLES20.GL_FLOAT, false, 0, 0); 

     GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 

     GLES20.glDisableVertexAttribArray(AttributeLocations.POSITION); 
     GLES20.glDisableVertexAttribArray(AttributeLocations.TEXTURE_COORDINATES); 
*/ 
     GLES20.glDisable(GLES20.GL_BLEND); 
     GLES20.glDisable(GLES20.GL_DEPTH_TEST); 
    } 

    @Override 
    public void setProjectionMatrix(float[] projectionMatrix) { 
     this.projectionMatrix = projectionMatrix; 
    } 

    public void setWatermarkTexture(Bitmap bitmap) { 
     watermark = bitmap; 
    } 
} 
+2

"작동하지 않는다"는 질문이 아닙니다. 무슨 일이 일어날 것으로 예상됩니까? 실제로는 어떻게됩니까? 차이점이 뭐야? 충돌이 발생하거나 잘못된 출력을 표시합니까? 로그가 무너지면? Yu는 더 철저하게 상황을 설명해야합니다. –

+0

방금 ​​만든 클래스 렌더러 클래스를 보았습니다. 그리고 위 클래스를 사용할 때 아무 일도 없었습니다. 그래서 비디오 미리보기 위에 png 워터 마크를 표시 할 수있는 텍스처 렌더러를 만들고 싶습니다. FYI 카메라 렌더러가 이미 작동 중입니다. 그러나 워터 마크를 보여주지는 않습니다. –

+0

그리고 내 소스 코드를 업데이트했는데 기본적으로 내 수업에서 누락 된 것이 무엇인지 알고 싶습니까? –

답변

0

내가 해결 내 문제.

다음은 소스 코드입니다.

public class WatermarkRenderer implements Renderer { 

    private final float[] TEX_VERTICES = { 
      0.0f, 1.0f, 
      1.0f, 1.0f, 
      0.0f, 0.0f, 
      1.0f, 0.0f 
    }; 
    private final float[] POS_VERTICES = { 
      0.2f, -1.0f, 
      1.0f, -1.0f, 
      0.2f, -0.9f, 
      1.0f, -0.9f 
    }; 

    private static final String VERTEX_SHADER = 
        "attribute vec4 a_position;\n" + 
        "attribute vec2 a_texcoord;\n" + 
        "varying vec2 v_texcoord;\n" + 
        "void main() {\n" + 
        " gl_Position = a_position;\n" + 
        " v_texcoord = a_texcoord;\n" + 
        "}\n"; 
    private static final String FRAGMENT_SHADER = 
        "precision mediump float;\n" + 
        "uniform sampler2D tex_sampler;\n" + 
        "varying vec2 v_texcoord;\n" + 
        "void main() {\n" + 
        " gl_FragColor = texture2D(tex_sampler, v_texcoord);\n" + 
        "}\n"; 

    private final int FLOAT_SIZE_BYTES = 4; 

    private Bitmap watermark; 

    private int shaderProgram; 
    private int texSamplerHandle; 
    private int texCoordHandle; 
    private int posCoordHandle; 
    private FloatBuffer texVertices; 
    private FloatBuffer posVertices; 

    private int[] textureHandles = new int[1]; 

    @Override 
    public void configureOpenGL() { 

     shaderProgram = GLES20.glCreateProgram(); 
     GLES20.glAttachShader(shaderProgram, OpenGLUtils.loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER)); 
     GLES20.glAttachShader(shaderProgram, OpenGLUtils.loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER)); 
     GLES20.glLinkProgram(shaderProgram); 

     int[] linkStatus = new int[1]; 
     GLES20.glGetProgramiv(shaderProgram, GLES20.GL_LINK_STATUS, linkStatus, 0); 
     if (linkStatus[0] != GLES20.GL_TRUE) { 
      String info = GLES20.glGetProgramInfoLog(shaderProgram); 
      GLES20.glDeleteProgram(shaderProgram); 
      shaderProgram = 0; 
      throw new RuntimeException("Could not link program: " + info); 
     } 

     texSamplerHandle = GLES20.glGetUniformLocation(shaderProgram, "tex_sampler"); 
     texCoordHandle = GLES20.glGetAttribLocation(shaderProgram, "a_texcoord"); 
     posCoordHandle = GLES20.glGetAttribLocation(shaderProgram, "a_position"); 
    } 

    @Override 
    public void createBuffers() { 
     ByteBuffer byteBuffer; 

     byteBuffer = ByteBuffer.allocateDirect(TEX_VERTICES.length * FLOAT_SIZE_BYTES); 
     byteBuffer.order(ByteOrder.nativeOrder()); 
     texVertices = byteBuffer.asFloatBuffer(); 
     texVertices.put(TEX_VERTICES); 
     texVertices.position(0); 

     byteBuffer = ByteBuffer.allocateDirect(POS_VERTICES.length * FLOAT_SIZE_BYTES); 
     byteBuffer.order(ByteOrder.nativeOrder()); 
     posVertices = byteBuffer.asFloatBuffer(); 
     posVertices.put(POS_VERTICES); 
     posVertices.position(0); 
    } 

    @Override 
    public void draw() { 

     // Select the program. 
     GLES20.glUseProgram(shaderProgram); 

     GLES20.glEnable(GLES20.GL_BLEND); 
     GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 

     // Set the vertex attributes 
     GLES20.glVertexAttribPointer(texCoordHandle, 2, GLES20.GL_FLOAT, false, 0, texVertices); 
     GLES20.glEnableVertexAttribArray(texCoordHandle); 
     GLES20.glVertexAttribPointer(posCoordHandle, 2, GLES20.GL_FLOAT, false, 0, posVertices); 
     GLES20.glEnableVertexAttribArray(posCoordHandle); 

     // Set the input texture 
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandles[0]); 
     GLES20.glUniform1i(texSamplerHandle, 0); 

     GLES20.glGenTextures(textureHandles.length, textureHandles, 0); 
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandles[0]); 

     GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, watermark, 0); 

     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 
     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); 
     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); 
     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); 

     GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 

     GLES20.glDisableVertexAttribArray(texCoordHandle); 
     GLES20.glDisableVertexAttribArray(posCoordHandle); 

     GLES20.glDisable(GLES20.GL_BLEND); 

     GLES20.glUseProgram(0); 
    } 

    @Override 
    public void setProjectionMatrix(float[] projectionMatrix) { 
    } 

    public void setWatermarkTexture(Bitmap bitmap) { 
     watermark = bitmap; 
    } 
}