关于滤镜
来源:互联网 发布:中国玩具出口数据 编辑:程序博客网 时间:2024/05/16 08:17
/解码算法 YUV to RGB //Method from Ketai project! void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) { final int frameSize = width * height; for (int j = 0, yp = 0; j < height; j++) { int uvp = frameSize + (j >> 1) * width, u = 0, v = 0; for (int 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); } } }创建surfaceView显示摄像头画面public class Preview extends SurfaceView implements SurfaceHolder.Callback{ private static final String TAG = "Preview"; SurfaceHolder mHolder; Camera mCamera; Filter filter; FilterFunction filterFunction; //This variable is responsible for getting and setting the camera settings private Parameters parameters; //this variable stores the camera preview size private Size previewSize; //this array stores the pixels as hexadecimal pairs private int[] pixels; public Preview(Context context) { super(context); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mHolder.addCallback(this); filterFunction = new FilterFunction(); filter = new Filter(); } public void surfaceCreated(SurfaceHolder holder) { if(mCamera == null){ // The Surface has been created, acquire the camera and tell it where // to draw. mCamera = Camera.open(); mCamera.setPreviewCallback(mPreviewCallback); try { mCamera.setPreviewDisplay(holder); ///initialize the variables parameters = mCamera.getParameters(); previewSize = parameters.getPreviewSize(); pixels = new int[previewSize.width * previewSize.height]; }catch (Exception e) { mCamera.release(); mCamera = null; Log.v(TAG, e.getLocalizedMessage()); } } } public void surfaceDestroyed(SurfaceHolder holder) { // Surface will be destroyed when we return, so stop the preview. // Because the CameraDevice object is not a shared resource, it's very // important to release it when the activity is paused. mCamera.stopPreview(); mCamera.release(); mCamera = null; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Camera.Parameters parameters = mCamera.getParameters();// 获得相机参数 parameters.setPreviewSize(320, 240); // 设置预览图像大小 parameters.setPictureFormat(PixelFormat.JPEG); // 设置照片格式 mCamera.setParameters(parameters);// 设置相机参数 mCamera.startPreview(); } private PreviewCallback mPreviewCallback = new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera camera) { if(data != null) { int imageWidth = mCamera.getParameters().getPreviewSize().width; int imageHeight = mCamera.getParameters().getPreviewSize().height; int RGBData[] = new int[imageWidth * imageHeight]; decodeYUV420SP(RGBData, data, imageWidth, imageHeight); // 解码 bm = Bitmap.createBitmap(RGBData, imageWidth, imageHeight, Config.ARGB_8888); //滤镜处理 } }};