natario1 / CameraView

📸 A well documented, high-level Android interface that makes capturing pictures and videos easy, addressing all of the common issues and needs. Real-time filters, gestures, watermarks, frame processing, RAW, output of any size.
https://natario1.github.io/CameraView
Other
4.94k stars 932 forks source link

cameraView.setFilter but screen is black #1231

Closed zhaichuankai closed 1 year ago

zhaichuankai commented 1 year ago

Describe the bug

Please add a clear description of what the bug is, and fill the list below.

Expected behavior

when i add a custom filter,but the screen is black

XML layout

Part of the XML layout with the CameraView declaration, so we can read its attributes.

        <com.otaliastudios.cameraview.CameraView
            android:id="@+id/camera"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:keepScreenOn="true"
            app:cameraAudio="off"
            app:cameraAutoFocusMarker="@string/cameraview_default_autofocus_marker"
            app:cameraEngine="camera1"
            app:cameraExperimental="true"
            app:cameraFacing="back"
            app:cameraFlash="off"
            app:cameraGestureLongTap="none"
            app:cameraGesturePinch="zoom"
            app:cameraGestureScrollHorizontal="filterControl1"
            app:cameraGestureScrollVertical="exposureCorrection"
            app:cameraGestureTap="autoFocus"
            app:cameraGrid="off"
            app:cameraMode="picture"
            app:cameraPlaySounds="true"
            app:cameraPreview="glSurface"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1.33"
            app:layout_constraintTop_toTopOf="parent">

        </com.otaliastudios.cameraview.CameraView>

my filter:

import android.opengl.GLES20;

import androidx.annotation.NonNull;

import com.otaliastudios.cameraview.filter.BaseFilter;

import java.nio.FloatBuffer;

public class GPUImageBeautyFilter extends BaseFilter { public static final String BILATERAL_FRAGMENT_SHADER = "" + " varying highp vec2 vTextureCoord;\n" + "\n" + " uniform sampler2D inputImageTexture;\n" + "\n" + " uniform highp vec2 singleStepOffset;\n" + " uniform highp vec4 params;\n" + " uniform highp float brightness;\n" + "\n" + " const highp vec3 W = vec3(0.299, 0.587, 0.114);\n" + " const highp mat3 saturateMatrix = mat3(\n" + " 1.1102, -0.0598, -0.061,\n" + " -0.0774, 1.0826, -0.1186,\n" + " -0.0228, -0.0228, 1.1772);\n" + " highp vec2 blurCoordinates[24];\n" + "\n" + " highp float hardLight(highp float color) {\n" + " if (color <= 0.5)\n" + " color = color color 2.0;\n" + " else\n" + " color = 1.0 - ((1.0 - color)(1.0 - color) 2.0);\n" + " return color;\n" + "}\n" + "\n" + " void main(){\n" + " highp vec3 centralColor = texture2D(inputImageTexture, vTextureCoord).rgb;\n" + " blurCoordinates[0] = vTextureCoord.xy + singleStepOffset vec2(0.0, -10.0);\n" + " blurCoordinates[1] = vTextureCoord.xy + singleStepOffset vec2(0.0, 10.0);\n" + " blurCoordinates[2] = vTextureCoord.xy + singleStepOffset vec2(-10.0, 0.0);\n" + " blurCoordinates[3] = vTextureCoord.xy + singleStepOffset vec2(10.0, 0.0);\n" + " blurCoordinates[4] = vTextureCoord.xy + singleStepOffset vec2(5.0, -8.0);\n" + " blurCoordinates[5] = vTextureCoord.xy + singleStepOffset vec2(5.0, 8.0);\n" + " blurCoordinates[6] = vTextureCoord.xy + singleStepOffset vec2(-5.0, 8.0);\n" + " blurCoordinates[7] = vTextureCoord.xy + singleStepOffset vec2(-5.0, -8.0);\n" + " blurCoordinates[8] = vTextureCoord.xy + singleStepOffset vec2(8.0, -5.0);\n" + " blurCoordinates[9] = vTextureCoord.xy + singleStepOffset vec2(8.0, 5.0);\n" + " blurCoordinates[10] = vTextureCoord.xy + singleStepOffset vec2(-8.0, 5.0);\n" + " blurCoordinates[11] = vTextureCoord.xy + singleStepOffset vec2(-8.0, -5.0);\n" + " blurCoordinates[12] = vTextureCoord.xy + singleStepOffset vec2(0.0, -6.0);\n" + " blurCoordinates[13] = vTextureCoord.xy + singleStepOffset vec2(0.0, 6.0);\n" + " blurCoordinates[14] = vTextureCoord.xy + singleStepOffset vec2(6.0, 0.0);\n" + " blurCoordinates[15] = vTextureCoord.xy + singleStepOffset vec2(-6.0, 0.0);\n" + " blurCoordinates[16] = vTextureCoord.xy + singleStepOffset vec2(-4.0, -4.0);\n" + " blurCoordinates[17] = vTextureCoord.xy + singleStepOffset vec2(-4.0, 4.0);\n" + " blurCoordinates[18] = vTextureCoord.xy + singleStepOffset vec2(4.0, -4.0);\n" + " blurCoordinates[19] = vTextureCoord.xy + singleStepOffset vec2(4.0, 4.0);\n" + " blurCoordinates[20] = vTextureCoord.xy + singleStepOffset vec2(-2.0, -2.0);\n" + " blurCoordinates[21] = vTextureCoord.xy + singleStepOffset vec2(-2.0, 2.0);\n" + " blurCoordinates[22] = vTextureCoord.xy + singleStepOffset vec2(2.0, -2.0);\n" + " blurCoordinates[23] = vTextureCoord.xy + singleStepOffset vec2(2.0, 2.0);\n" + "\n" + " highp float sampleColor = centralColor.g 22.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[0]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[1]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[2]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[3]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[4]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[5]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[6]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[7]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[8]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[9]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[10]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[11]).g;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[12]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[13]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[14]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[15]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[16]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[17]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[18]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[19]).g 2.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[20]).g 3.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[21]).g 3.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[22]).g 3.0;\n" + " sampleColor += texture2D(inputImageTexture, blurCoordinates[23]).g 3.0;\n" + "\n" + " sampleColor = sampleColor / 62.0;\n" + "\n" + " highp float highPass = centralColor.g - sampleColor + 0.5;\n" + "\n" + " for (int i = 0; i < 5; i++) {\n" + " highPass = hardLight(highPass);\n" + " }\n" + " highp float lumance = dot(centralColor, W);\n" + "\n" + " highp float alpha = pow(lumance, params.r);\n" + "\n" + " highp vec3 smoothColor = centralColor + (centralColor-vec3(highPass))alpha0.1;\n" + "\n" + " smoothColor.r = clamp(pow(smoothColor.r, params.g), 0.0, 1.0);\n" + " smoothColor.g = clamp(pow(smoothColor.g, params.g), 0.0, 1.0);\n" + " smoothColor.b = clamp(pow(smoothColor.b, params.g), 0.0, 1.0);\n" + "\n" + " highp vec3 lvse = vec3(1.0)-(vec3(1.0)-smoothColor)(vec3(1.0)-centralColor);\n" + " highp vec3 bianliang = max(smoothColor, centralColor);\n" + " highp vec3 rouguang = 2.0centralColorsmoothColor + centralColorcentralColor - 2.0centralColorcentralColorsmoothColor;\n" + "\n" + " gl_FragColor = vec4(mix(centralColor, lvse, alpha), 1.0);\n" + " gl_FragColor.rgb = mix(gl_FragColor.rgb, bianliang, alpha);\n" + " gl_FragColor.rgb = mix(gl_FragColor.rgb, rouguang, params.b);\n" + "\n" + " highp vec3 satcolor = gl_FragColor.rgb * saturateMatrix;\n" + " gl_FragColor.rgb = mix(gl_FragColor.rgb, satcolor, params.a);\n" + " gl_FragColor.rgb = vec3(gl_FragColor.rgb + vec3(brightness));\n" + "}";

private float toneLevel = 0.47f;
private float beautyLevel = 0.42f;
private float brightLevel = 0.34f;
private int width = 1;
private int height = 1;

private int paramsLocation;
private int brightnessLocation;
private int singleStepOffsetLocation;

public GPUImageBeautyFilter() { }

@Override
public void setSize(int width, int height) {
    super.setSize(width, height);
    this.width = width;
    this.height = height;
}

@NonNull
@Override
public String getFragmentShader() {
    return BILATERAL_FRAGMENT_SHADER;
}

@Override
public void onCreate(int programHandle) {
    super.onCreate(programHandle);
    paramsLocation = GLES20.glGetUniformLocation(programHandle, "params");
    brightnessLocation = GLES20.glGetUniformLocation(programHandle, "brightness");
    singleStepOffsetLocation = GLES20.glGetUniformLocation(programHandle, "singleStepOffset");
}

@Override
public void onDestroy() {
    super.onDestroy();
    paramsLocation = -1;
    brightnessLocation = -1;
    singleStepOffsetLocation = -1;
}

@Override
protected void onPreDraw(long timestampUs, @NonNull float[] transformMatrix) {
    super.onPreDraw(timestampUs, transformMatrix);
    float[] vector = new float[4];
    vector[0] = 1.0f - 0.6f * beautyLevel;
    vector[1] = 1.0f - 0.3f * beautyLevel;
    vector[2] = 0.1f + 0.3f * toneLevel;
    vector[3] = 0.1f + 0.3f * toneLevel;
    GLES20.glUniform2fv(singleStepOffsetLocation, 1, FloatBuffer.wrap(new float[] {2.0f / width, 2.0f / height}));
    GLES20.glUniform4fv(paramsLocation, 1, FloatBuffer.wrap(vector));
    GLES20.glUniform1f(brightnessLocation, 0.6f * (-0.5f + brightLevel));
}

}

zhaichuankai commented 1 year ago

yeah i sloved it

zhaichuankai commented 1 year ago

For advanced users, child filters need to read from GLES20.GL_TEXTURE_2D instead of the typical GLES11Ext.GL_TEXTURE_EXTERNAL_OES. To achieve this, we get your fragment shader String and replace any "samplerExternalOES" with "sampler2D". This is a hack that might cause issues with specific shader implementations.

zhaichuankai commented 1 year ago

replace sampler2D to samplerExternalOES and add "#extension GL_OES_EGL_image_external : require\n"

353338110 commented 1 year ago

replace sampler2D to samplerExternalOES and add "#extension GL_OES_EGL_image_external : require\n"

I also do this,but it still balck.can you give complete code