Search in sources :

Example 1 with AutoFocusCallback

use of android.hardware.Camera.AutoFocusCallback in project JustAndroid by chinaltz.

the class CaptureActivity method onCreate.

/**
 * 开始.
 * @param savedInstanceState
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // 屏幕参数
    int cameraId = this.getIntent().getIntExtra("cameraId", 0);
    int orientation = this.getIntent().getIntExtra("orientation", 0);
    Config.cameraId = cameraId;
    Config.orientation = orientation;
    Config.focusMode = 1;
    // 强制为横屏
    if (Config.orientation == 0) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    setContentView(R.layout.camera_main);
    // 初始化 CameraManager
    CameraManager.init(getApplication());
    surfaceView = (SurfaceView) findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = (SurfaceHolder) surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceView.setKeepScreenOn(true);
    // UI相关
    camBtn = (Button) this.findViewById(R.id.shot_btn);
    flashBtn = (Button) this.findViewById(R.id.flash_btn);
    focusBtn = (Button) this.findViewById(R.id.focus_btn);
    okBtn = (Button) this.findViewById(R.id.ok_btn);
    cancleBtn = (Button) this.findViewById(R.id.cancle_btn);
    camLayout = (FrameLayout) this.findViewById(R.id.cam_layout);
    previewLayout = (FrameLayout) this.findViewById(R.id.preview_layout);
    camLayout.setVisibility(View.VISIBLE);
    previewLayout.setVisibility(View.GONE);
    imgResult = (ImageView) this.findViewById(R.id.imgResult);
    if (cameraId == 1) {
        flashBtn.setVisibility(View.GONE);
    }
    okBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("path", path);
            setResult(RESULT_OK, intent);
            finish();
        }
    });
    // 拍照的回调
    CameraManager.get().setPictureCallback(new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // 关闭预览
            CameraManager.get().stopPreview();
            Bitmap cameraBitmap = AbImageUtil.getBitmap(data, 1280, 720);
            camLayout.setVisibility(View.INVISIBLE);
            previewLayout.setVisibility(View.VISIBLE);
            imgResult.setImageBitmap(cameraBitmap);
            // 插入到相册数据库
            try {
                String url = insertImage(CaptureActivity.this.getContentResolver(), cameraBitmap, "andbase", "andbase");
                path = getRealFilePath(CaptureActivity.this, Uri.parse(url));
                Log.e("onPictureTaken", "onPictureTaken insertImage:" + path);
                AbToastUtil.showToast(CaptureActivity.this, "insertImage:" + path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    // 对焦的控制
    CameraManager.get().setAutoFocusCallback(new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            if (success) {
            // 对焦成功
            } else {
            /*//关键代码:自动对焦
					new Handler().postDelayed(new Runnable(){

						@Override
						public void run() {
							CameraManager.get().requestAutoFocus();
						}
						
					}, 1000);*/
            }
            if (Config.focusMode == 1) {
                CameraManager.get().startPreview();
            }
        }
    });
    // 预览
    CameraManager.get().setPreviewCallback(new PreviewCallback() {

        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
        }
    });
    // 拍照
    camBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            CameraManager.get().takePicture();
            // 声音
            shootSound();
        }
    });
    // 点击屏幕
    surfaceView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            imgResult.setImageBitmap(null);
            // 继续预览
            CameraManager.get().startPreview();
        }
    });
    // 闪光灯
    flashBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            CameraManager.get().toogleFlash();
        }
    });
    if (Config.focusMode == 0) {
        // 手动对焦
        focusBtn.setVisibility(View.VISIBLE);
        focusBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CameraManager.get().requestAutoFocus();
            }
        });
    }
    cancleBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            imgResult.setImageBitmap(null);
            camLayout.setVisibility(View.VISIBLE);
            previewLayout.setVisibility(View.INVISIBLE);
            CameraManager.get().startPreview();
        }
    });
}
Also used : PictureCallback(android.hardware.Camera.PictureCallback) Intent(android.content.Intent) SurfaceView(android.view.SurfaceView) ImageView(android.widget.ImageView) View(android.view.View) SurfaceHolder(android.view.SurfaceHolder) Bitmap(android.graphics.Bitmap) PreviewCallback(android.hardware.Camera.PreviewCallback) OnClickListener(android.view.View.OnClickListener) Camera(android.hardware.Camera) AutoFocusCallback(android.hardware.Camera.AutoFocusCallback)

Aggregations

Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 Camera (android.hardware.Camera)1 AutoFocusCallback (android.hardware.Camera.AutoFocusCallback)1 PictureCallback (android.hardware.Camera.PictureCallback)1 PreviewCallback (android.hardware.Camera.PreviewCallback)1 SurfaceHolder (android.view.SurfaceHolder)1 SurfaceView (android.view.SurfaceView)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ImageView (android.widget.ImageView)1