Flash引领了android

时间:2013-12-09 07:36:27

标签: android flash led

我在flash led中遇到了问题。当我在2秒后开启闪光灯时,闪光灯自动关闭。我不知道为什么会这样。谁能有任何想法请帮助我。

public void cameraFlashOn() {
try {
    camera = Camera.open();
    Parameters params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_ON);
    camera.setParameters(params);
    camera.startPreview();
    camera.autoFocus(new AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
        }
    });
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}  

关闭flash我使用了以下代码

public void cameraFlashOff() {
try {
    camera.stopPreview();
    camera.release();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

1 个答案:

答案 0 :(得分:0)

package com.mkyong.android;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FlashLightActivity extends Activity {

    //flag to detect flash is on or off
    private boolean isLighOn = false;

    private Camera camera;

    private Button button;

    @Override
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.release();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonFlashlight);

        Context context = this;
        PackageManager pm = context.getPackageManager();

        // if device support camera?
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }

        camera = Camera.open();
        final Parameters p = camera.getParameters();

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (isLighOn) {

                    Log.i("info", "torch is turn off!");

                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(p);
                    camera.stopPreview();
                    isLighOn = false;

                } else {

                    Log.i("info", "torch is turn on!");

                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);

                    camera.setParameters(p);
                    camera.startPreview();
                    isLighOn = true;

                }

            }
        });

    }
}