Android Camera SurfaceView

这个类图以camera预览为场景。从类图中可以看出,

  1. SurfaceView继承自(is-a)View
  2. SurfaceView中有一个SurfaceHolder,可以通过getHolder得到这个holder,这个holder有一个方法addCallback(Callback callback),可以向这个类中注册一个Callback(MainActivity),并且SurfaceHolder中有一个(has-a)Surface。
  3. Camera中通过setPreviewDisplay(SurfaceHolder holder),向Camera注册一个SurfaceHolder
  4. 这样底层的Camera就可以将图像渲染在SurfaceHolder的Surface中
  5. 最后SurfaceFlinger会将所有应用的Surface进行混合,我们在手机看到的,就是最终混合的效果。
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

import com.lemniscate.audio_and_video_demo.R;

import java.io.IOException;
import java.util.List;

public class SurfaceViewCameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private SurfaceView sv;
    private SurfaceHolder holder;
    private Camera mCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_surface_view_camera);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        sv = findViewById(R.id.sv);
        holder = sv.getHolder();
        holder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = getCamera();
        if (mCamera != null) {
            try {
                mCamera.setPreviewDisplay(holder);//预览数据送到holder
                mCamera.startPreview();
                mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                    @Override
                    public void onPreviewFrame(byte[] data, Camera camera) {
                          //得到的预览数据
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //这里变化的时候转换方向 等
        refreshCamera();
        int displayOrientation = getDisplayOrientation();
        mCamera.setDisplayOrientation(displayOrientation);
        Camera.Parameters params = mCamera.getParameters();
        Camera.Size optimalPreviewSize = getOptimalPreviewSize(mCamera.getParameters().getSupportedPreviewSizes(), width, height);
        params.setPictureSize(optimalPreviewSize.width, optimalPreviewSize.height);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        holder.removeCallback(this);
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

//获取最佳的分辨率 而且是16:9的
    private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.75;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;

        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Camera.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }
    //这里是一小段算法算出摄像头转多少都和屏幕方向一致
    private int getDisplayOrientation() {
        WindowManager windowManager = getWindowManager();
        Display defaultDisplay = windowManager.getDefaultDisplay();
        int orientation = defaultDisplay.getOrientation();
        int degress = 0;
        switch (orientation) {
            case Surface.ROTATION_0:
                degress = 0;
                break;
            case Surface.ROTATION_90:
                degress = 90;
                break;
            case Surface.ROTATION_180:
                degress = 180;
                break;
            case Surface.ROTATION_270:
                degress = 270;
                break;
        }
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, cameraInfo);
        int result = (cameraInfo.orientation - degress + 360) % 360;
        return result;
    }

    private void refreshCamera() {
        if (holder.getSurface() == null) {
            //preview surface does not exist
            return;
        }
        mCamera.stopPreview();

        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Camera getCamera() {
        return Camera.open();
    }
}

主要步骤如下:

  1. SurfaceHolder.addCallback。
  2. 在android.view.SurfaceHolder.Callback#surfaceCreated方法中,初始化摄像头相关。
  3. 通过android.hardware.Camera#setPreviewDisplay 来绑定SurfaceHolder。
  4. 调用android.hardware.Camera#startPreview来开始摄像。

这种情况下,视频直接渲染到surfaceView上边。如果要进行特殊操作。

  1. 通过android.hardware.Camera#setPreviewCallback监听每一帧的回调。
  2. 在回调中调用方法直接绘制到surfaceView上。
      Canvas canvas = surfaceHolder.lockCanvas();
        canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
        Bitmap cacheBitmap = nv21ToBitmap(data, width, height);
        canvas.drawBitmap(cacheBitmap, 0, 0, null);
        surfaceHolder.unlockCanvasAndPost(canvas);
 private static Bitmap nv21ToBitmap(byte[] nv21, int width, int height) {
        Bitmap bitmap = null;
        try {
            YuvImage image = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compressToJpeg(new Rect(0, 0, width, height), 80, stream);
            bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

 

 

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页