Android自定义相机滤镜

目录

效果展示

关键流程

关键代码

1.设置相机不显示预览

这里由于我们需要处理和显示的是相机回调的数据,因此我们并不需要进行预览,所以我们可以通过Camera的setPreviewTexture方法实现,如下所示

SurfaceTexture surfaceTexture = new SurfaceTexture(GL11Ext.GL_TEXTURE_CROP_RECT_OES);
mCamera.setPreviewTexture(surfaceTexture);
2.处理回调数据

这里的逻辑与我们上面展示的关键流程图相对应,如下所示

cameraHelper.setFrameCallback(new CameraHelper.FrameCallback() {
            @Override
            public void onDecodeFrame(byte[] data,final Camera camera) {
                try {
                    mCanvas=mHolder.lockCanvas();
                    //初始化画布并在画布上画一些东西
                    int width = camera.getParameters().getPreviewSize().width;
                    int height = camera.getParameters().getPreviewSize().height;
                    int[] result = yuv2Rgb(data,width,height);
                    if(matrix == null){
                        matrix = new Matrix();
                        matrix.preScale(mCanvas.getWidth()/1.0f/height,mCanvas.getHeight()/1.0f/width);
                    }
                    Bitmap bitmapTemp = Bitmap.createBitmap(result, height, width, Bitmap.Config.RGB_565);
                    bitmapTemp = Bitmap.createBitmap(bitmapTemp, 0,0,height,width, matrix, false);
                    gpuImage.setImage(bitmapTemp);
                    bitmap = gpuImage.getBitmapWithFilterApplied();
                    mCanvas.drawBitmap(bitmap,0,0,paint);
                    bitmapTemp.recycle();
                }catch (Exception e){
                }finally {
                    //判断画布是否为空,从而避免黑屏情况
                    if(mCanvas!=null){
                        mHolder.unlockCanvasAndPost(mCanvas);
                    }
                    camera.addCallbackBuffer(data);
                }
            }
        });

JNI函数如下

#include 
#include 
#include 
extern "C"
JNIEXPORT jintArray JNICALL
Java_com_itfitness_filter_MainActivity_yuv2Rgb(JNIEnv *env, jobject thiz,
                                               jbyteArray buf, jint width,
                                               jint height) {
    jbyte *yuv420spSRC = (env)->GetByteArrayElements(buf, 0);
    jbyte *yuv420sp = (jbyte *)malloc(env->GetArrayLength(buf));

    //旋转图像(因为传过来的图像是横着的)
    int index = 0;
    //旋转Y分量,放入dst数组
    for (int y = 0; y < width; y++)
        for (int x = 0; x < height; x++) {
            int oldY = (height - 1) - x;
            int oldX = y;
            int oldIndex = oldY * width + oldX;
            yuv420sp[index++] = yuv420spSRC[oldIndex];
        }
    //每四个点采集一组VU分量,共享右上角像素的VU分量
    //根据Y分量,找到对应的VU分量,放入dst数组
    for (int y = 0; y < width; y += 2)
        for (int x = 0; x < height; x += 2) {
            int oldY = (height - 1) - (x + 1);
            int oldX = y;
            int vuY = height + oldY / 2; //根据Y分量计算VU分量所在行
            int vuX = oldX;
            int vuIndex = vuY * width + vuX;
            yuv420sp[index++] = yuv420spSRC[vuIndex];
            yuv420sp[index++] = yuv420spSRC[vuIndex + 1];
        }

    //将YUV转为RGB
    int frameSize = width * height;
    jint rgb[frameSize];
    int i = 0, j = 0, yp = 0;
    int uvp = 0, u = 0, v = 0;
    for (j = 0, yp = 0; j < height; j++) {
        uvp = frameSize + (j >> 1) * width;
        u = 0;
        v = 0;
        for (i = 0; i < width; i++, yp++) {
            int y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0)
                y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0) r = 0; else if (r > 262143) r = 262143;
            if (g < 0) g = 0; else if (g > 262143) g = 262143;
            if (b < 0) b = 0; else if (b > 262143) b = 262143;

            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
        }
    }
    jintArray result = (env)->NewIntArray(frameSize);
    (env)->SetIntArrayRegion(result, 0, frameSize, rgb);
    free(yuv420sp);
    (env)->ReleaseByteArrayElements(buf, yuv420spSRC, 0);
    return result;
}

另外我通过按钮的点击来切换GpuImage的滤镜

 bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                opera++;
                if(opera > 3){
                    opera = 0;
                }
                switch (opera){
                    case 0:
                        gpuImage.setFilter(new GPUImageDissolveBlendFilter());
                        break;
                    case 1:
                        //素描
                        gpuImage.setFilter(new GPUImageSketchFilter());
                        break;
                    case 2:
                        //水彩
                        gpuImage.setFilter(new GPUImageToonFilter());
                        break;
                    case 3:
                        //二值化
                        gpuImage.setFilter(new GPUImageThresholdEdgeDetection());
                        break;
                }
            }
        });

案例源码

详细的代码可以下载源码查看:https://gitee.com/itfitness/camera-filter

你可能感兴趣的:(Android自定义相机滤镜)