Android:在CameraPrevie中绘制相框

时间:2014-02-13 21:24:26

标签: image android-camera surfaceview

这是我使用CameraPreview拍摄照片的程序。我想放一个相框,我该怎么做?

这是我的代码

public class CameraPreview extends FrameLayout implements SurfaceHolder.Callback{

    //Vista sobre la que se mostrara la vista previa
    private SurfaceView surfaceView;

    //Cámara
    private Camera camera;

    //Identificadores de la camara

    private final int REAR_CAMERA = 0;
    private final int FRONT_CAMERA = 1;
    private int activeCameraId = REAR_CAMERA;

    public CameraPreview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

        createCamera();

        if(camera==null){
            return;
        }

        //crear la vista y añadirla al layout
        surfaceView = new SurfaceView(context);
        addView(surfaceView);

        //añadimos el escuchador a la vista
        SurfaceHolder holder = surfaceView.getHolder();
        holder.addCallback(this);

        //Cosas del sdk antiguo
        //holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //holder.setKeepScreenOn(true);

    }

    private void createCamera(){
        //Si ya habia camara la libero
        if(camera!=null){
            camera.stopPreview();
            camera.release();
        }

        //Compruebo si hay camara
        if (getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            try{
                //obtengo las instancia de la camara
                this.camera= Camera.open(activeCameraId);
            }catch(Exception e){
                Toast.makeText(getContext(), "Error al abrir la camara", Toast.LENGTH_LONG).show();
            }
        } else{
            Toast.makeText(getContext(), "No hay camara", Toast.LENGTH_LONG).show();

        }
    }

    public Camera getCamera(){
        return camera;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
        if(holder.getSurface()==null){
            return;
        }

        camera.stopPreview();

        Display display =((WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        CameraInfo info= new Camera.CameraInfo();

        Camera.getCameraInfo(0, info);
        int cameraOrientation= info.orientation;

        Size cameraSize = camera.getParameters().getPreviewSize();

        if((display.getRotation()== Surface.ROTATION_0||display.getRotation()==Surface.ROTATION_180)&&
                (cameraOrientation==90||cameraOrientation==270)){
            cameraSize.width=camera.getParameters().getPreviewSize().height;
            cameraSize.height=camera.getParameters().getPreviewSize().width;
            camera.setDisplayOrientation(90);
        }

        float ratio = (float) cameraSize.width/(float) cameraSize.height;

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((int) (height*ratio),height,Gravity.CENTER);
        surfaceView.setLayoutParams(params);

        try{
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        }catch(Exception e){
            Log.d("CameraPreview","Error al mostrar la vista previa: "+ e.getMessage());
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        try{
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        }catch (IOException e){
            Log.d("CameraPreview", "Error al poner la vista previa de la camare" + e.getMessage());     
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

        camera.stopPreview();

        camera.release();
        camera= null;

        holder.removeCallback(this);

    }
}

1 个答案:

答案 0 :(得分:0)

您可以在surfaceView之上添加其他视图(或布局)。见Unable to draw on top of a custom SurfaceView class