目錄
效果展示
關(guān)鍵流程
關(guān)鍵代碼
1.設(shè)置相機不顯示預(yù)覽
這里由于我們需要處理和顯示的是相機回調(diào)的數(shù)據(jù),因此我們并不需要進行預(yù)覽挂捻,所以我們可以通過Camera的setPreviewTexture方法實現(xiàn)辐真,如下所示
SurfaceTexture surfaceTexture = new SurfaceTexture(GL11Ext.GL_TEXTURE_CROP_RECT_OES);
mCamera.setPreviewTexture(surfaceTexture);
2.處理回調(diào)數(shù)據(jù)
這里的邏輯與我們上面展示的關(guān)鍵流程圖相對應(yīng)返奉,如下所示
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函數(shù)如下
#include <jni.h>
#include <string>
#include <android/bitmap.h>
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));
//旋轉(zhuǎn)圖像(因為傳過來的圖像是橫著的)
int index = 0;
//旋轉(zhuǎn)Y分量,放入dst數(shù)組
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分量
//根據(jù)Y分量级乍,找到對應(yīng)的VU分量,放入dst數(shù)組
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; //根據(jù)Y分量計算VU分量所在行
int vuX = oldX;
int vuIndex = vuY * width + vuX;
yuv420sp[index++] = yuv420spSRC[vuIndex];
yuv420sp[index++] = yuv420spSRC[vuIndex + 1];
}
//將YUV轉(zhuǎn)為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