简体   繁体   中英

Camera app with Auto Focus and Flash Light

I am developing a camera app in android (without using intent) where , i am implementing both auto focus and Flash light features . It works fine if i implement any one feature. But on adding both Auto focus and Flash Light feature, it gives force close in LG nexus and other phones which has Flash Light. i am using the below code for auto focus and Flash Light.

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
    {
        event.startTracking();
        camera.autoFocus(autoFocusCallback);
        Parameters p = camera.getParameters();
        p.setFocusMode(Parameters.FOCUS_MODE_AUTO);

        if(this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
        {

            p.setFlashMode(Parameters.FLASH_MODE_ON);
            camera.setParameters(p);
            camera.startPreview();
            camera.takePicture(shutterCallback, rawCallback, jpgCallback);
        }

        else
        {
            camera.startPreview();
            camera.takePicture(shutterCallback, rawCallback, jpgCallback);
        }
        return true;

    }
    return super.onKeyDown(keyCode, event);
}

     private AutoFocusCallback autoFocusCallback = new AutoFocusCallback() 
    {

     @Override
     public void onAutoFocus(boolean autoFocusSuccess, Camera camera)
      {
            camera.takePicture(shutterCallback, rawCallback, jpgCallback);
              if (autoFocusHandler != null)
              {
                 Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success);
                 autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS);
                  autoFocusHandler = null;
               }
               else
               {

               }
    }
};

This is the Error Log i am getting.

http://txtup.co/WCYjl

What may be the problem. Please Help! Thanks!

Just had a quick read through the Camera docs, and it looks like you've missed the Surface Holder.

Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.

Have a read through the docs: http://developer.android.com/reference/android/hardware/Camera.html

Here is an answer which shows how to use this: https://stackoverflow.com/a/3964460/2045570

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM