1

我正在尝试制作一个在 Galaxy Nexus 上运行的闪光灯应用程序。我知道您需要实现 SurfaceHolder.Callback。但是当我这样做时,应用程序正在编译和运行,没有错误。但是 LED 根本没有打开,并且应用程序在按下切换按钮 3 次后崩溃。

public class MainActivity extends Activity implements SurfaceHolder.Callback {
Camera cam;
public static SurfaceHolder mHolder;
public static SurfaceView preview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preview = (SurfaceView) findViewById(R.id.PREVIEW);
    mHolder = preview.getHolder();
    cam = Camera.open();
    try {
        cam.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton1);
    final TextView text = (TextView) findViewById(R.id.textView1);
    text.setText("Flash Light");

    toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub


            if(isChecked) {

                text.setText("Flash Light is ON");
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();

            } else {

                text.setText("Flash Light is OFF");
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
                cam.release();

            }

        }

    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mHolder = holder;
    try {
        cam.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
    cam.stopPreview();
    mHolder = null;

}
}

编辑:清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flashlight"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.FLASHLIGHT"/> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-feature android:name="android.hardware.camera.Parameters" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:debuggable="true">
        <activity
            android:name="com.example.flashlight.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

回答

我发现在我需要在我的 SurfaceView 下添加的 activity_main.xml 中的最后一行:

activity_main.xml

<SurfaceView
android:id="@+id/PREVIEW"
android:layout_width="1dip"
android:layout_height="1dip"
android:visibility="visible" />
4

1 回答 1

0

检查 flashModes 是否可用。并且还会使您Camera mCamera static在方向更改时失去相机实例。

public static void FlashOn(Camera mCamera)
    {
        //Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        //Check Whether device supports AutoFlash, If you YES then set AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        }
        else if(flashModes.contains(Parameters.FLASH_MODE_ON))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

    public static void FlashOff(Camera mCamera)
    {
        //Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        //Check Whether device supports AutoFlash, If you YES then set AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_OFF))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
于 2015-04-14T12:59:38.267 回答