开启手电筒时出现异常 - 相机处于运行模式

时间:2013-12-17 11:23:59

标签: android flash camera flashlight

我在三星Galaxy相机上测试我的相机应用程序,但我正在尝试开启Flash 获取 开启手电筒时出现异常< / em> ,因为我在catch块中使用此消息..

  Note:- Camera always in running mode

为什么我无法在我的应用中 ON Flash ,我使用两个不同的按钮,一个用于 捕获图像 ,仅次于 控制闪光灯 (ON&amp; OFF)

行号 199 是:

   mCamera.setParameters(p);

行号 140 是:

   turnOnFlashLight();

CameraLauncherActivity.java -

            flashButton = (ImageButton) findViewById(R.id.btnFlash);
            Log.d(CameraLauncherActivity.LOG_TAG, "SingleAngelActivityButton :: " + viewButton);
            flashButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {             

                    if (isFlashOn) {
                        // turn off flash
                        turnOffFlashLight();
                    } else {
                        // turn on flash
                        turnOnFlashLight();
                    }
                }
            });

            cd = new ConnectionDetector(getApplicationContext());

            // Check if Internet present
            if (!cd.isConnectingToInternet()) {
                // Internet Connection is not present
                alert.showAlertDialog(CameraLauncherActivity.this, "Internet Connection Error",
                        "Please connect to working Internet connection", false);
                // stop executing code by return
                return;
            }

            checkFlash();

      // calling background thread
        new LoadSingleTrack().execute();
    }

@SuppressWarnings("deprecation")
public void checkFlash()
{
    /*
     * First check if device is supporting flashlight or not
     */
    hasFlash = getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        // device doesn't support flash
        // Show alert message and close the application
        AlertDialog alert = new AlertDialog.Builder(CameraLauncherActivity.this)
                .create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device doesn't support flash light!");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // closing the application
                finish();
            }
        });
        alert.show();
        return;
    }
}

public void turnOnFlashLight() {
    try {
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            Parameters p = mCamera.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(p);
            isFlashOn = true;   
            toggleButtonImage();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "Exception throws in turning on flashlight.", Toast.LENGTH_SHORT).show();
    }
}

public void turnOffFlashLight() {
    try {
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            isFlashOn = false;
            toggleButtonImage();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "Exception throws in turning off flashlight.", Toast.LENGTH_SHORT).show();
    }
}


private void toggleButtonImage(){
    if(isFlashOn){
        flashButton.setImageResource(R.drawable.green_button);
    }else{
        flashButton.setImageResource(R.drawable.red_button);
    }
}

     private Camera getCameraInstance() {

        Camera camera = null;

        try {
            camera = Camera.open(0);

        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        if(camera!=null){
         Camera.Parameters parameters = camera.getParameters();
         camera.setParameters(parameters);
        }
        return camera;
    }

PreviewSurface.java -

public class PreviewSurface extends SurfaceView implements
SurfaceHolder.Callback {

    public static final String LOG_TAG = "CameraPreview";
    private SurfaceHolder mSurfaceHolder;

    private Camera mCamera;

    // Constructor that obtains context and camera
    @SuppressWarnings("deprecation")
    public PreviewSurface(Context context, Camera camera) {
        super(context);
        this.mCamera = camera;

        this.mSurfaceHolder = this.getHolder();
        this.mSurfaceHolder.addCallback(this);
        this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.mSurfaceHolder.setFixedSize(100, 100);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {       
            Camera.Parameters parameters = mCamera.getParameters();
            if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) 
            {
                 parameters.set("orientation", "portrait");
                 mCamera.setDisplayOrientation(90);
                 parameters.setRotation(90);
                 mCamera.setPreviewDisplay(surfaceHolder);
                 mCamera.startPreview();
            }
            else 
            {
                 // This is an undocumented although widely known feature
                 parameters.set("orientation", "landscape");
                 // For Android 2.2 and above
                 mCamera.setDisplayOrientation(0);
                 // Uncomment for Android 2.0 and above
                 parameters.setRotation(0);
            }
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();

        } catch (IOException e) {
            // left blank for now
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mCamera.stopPreview();
        mCamera.release();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
            int width, int height) {

        try {       
            Camera.Parameters parameters = mCamera.getParameters();
            if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                 parameters.set("orientation", "portrait");
                 mCamera.setDisplayOrientation(90);
                 parameters.setRotation(90);

            }
                 else {
                      // This is an undocumented although widely known feature
                      parameters.set("orientation", "landscape");
                      // For Android 2.2 and above
                      mCamera.setDisplayOrientation(0);
                      // Uncomment for Android 2.0 and above
                      parameters.setRotation(0);
            }
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();

        } catch (IOException e) {
            // left blank for now
        }           
    }

}

日志说:

 12-17 18:12:56.485: W/System.err(18918): java.lang.RuntimeException: setParameters failed
12-17 18:12:56.485: W/System.err(18918):    at android.hardware.Camera.native_setParameters(Native Method)
12-17 18:12:56.485: W/System.err(18918):    at android.hardware.Camera.setParameters(Camera.java:1475)
12-17 18:12:56.485: W/System.err(18918):    at com.example.camera.CameraLauncherActivity.turnOnFlashLight(CameraLauncherActivity.java:199)
12-17 18:12:56.485: W/System.err(18918):    at com.example.camera.CameraLauncherActivity$4.onClick(CameraLauncherActivity.java:140)
12-17 18:12:56.485: W/System.err(18918):    at android.view.View.performClick(View.java:4223)
12-17 18:12:56.485: W/System.err(18918):    at android.view.View$PerformClick.run(View.java:17275)
12-17 18:12:56.485: W/System.err(18918):    at android.os.Handler.handleCallback(Handler.java:615)
12-17 18:12:56.485: W/System.err(18918):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 18:12:56.485: W/System.err(18918):    at android.os.Looper.loop(Looper.java:137)
12-17 18:12:56.485: W/System.err(18918):    at android.app.ActivityThread.main(ActivityThread.java:4921)
12-17 18:12:56.485: W/System.err(18918):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 18:12:56.485: W/System.err(18918):    at java.lang.reflect.Method.invoke(Method.java:511)
12-17 18:12:56.485: W/System.err(18918):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
12-17 18:12:56.485: W/System.err(18918):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
12-17 18:12:56.485: W/System.err(18918):    at dalvik.system.NativeStart.main(Native Method)
12-17 18:13:29.370: W/IInputConnectionWrapper(18918): getSelectedText on inactive InputConnection
12-17 18:13:29.375: W/IInputConnectionWrapper(18918): setComposingText on inactive InputConnection

0 个答案:

没有答案