在我们app中经常会调用相机进行拍照,然后把拍下来的图片保存在本地,再上传到服务端,网上有不少自定义的相机,也下载了不少,但是效果还是不满意,所以决定自定义下,不多说了,直接上代码:
public class CameraViews extends SurfaceView implements SurfaceHolder.Callback{
private String TAG=Camera.class.getName();
private Activity mActivity=null;
private Camera camera=null;
private SurfaceHolder holder=null;
/* 是否打开前置相机,true为前置,false为后置 */
private boolean mIsFrontCamera=false;
/是否开启闪光灯 默认关闭闪光灯/
private String isOpenFlashMode= Camera.Parameters.FLASH_MODE_OFF;
private int curRotation=0;
private CameraCallback mCallback=null;
public CameraViews(Context context){
super(context);
initSurface();
}
public CameraViews(Context context, AttributeSet attrs) {
super(context, attrs);
initSurface();
}
public void setCurActivity(Activity activity){
this.mActivity=activity;
}
/**
* @Description: 初始化surface
* @Since:2015-8-11
* @Version:1.1.0
*/
private void initSurface(){
holder =getHolder();//获得句柄
holder.addCallback(this);//添加回调
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//surfaceview不维护自己的缓冲区,等待屏幕渲染引擎将内容推送到用户面前
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if(camera==null){
openCamera();
}
initCameraParams(mActivity);
camera.setPreviewDisplay(getHolder());
camera.startPreview();
} catch (Exception e) {
Toast.makeText(getContext(), "打开相机失败", Toast.LENGTH_SHORT).show();
Log.e(TAG,e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
//try{
//camera.stopPreview();
//摄像头画面显示在Surface上
//camera.setPreviewDisplay(holder);
//initCameraPara