gpt4 book ai didi

java - 手电筒无法打开

转载 作者:太空宇宙 更新时间:2023-11-04 13:13:57 24 4
gpt4 key购买 nike

嗨,我遵循了教程 here并写出了代码,我测试了它,但手电筒没有打开,我不知道出了什么问题。我省略了有声音的部分,因为我希望没有声音。

package net.netne.benpaterson35;
import android.app.*;
import android.os.*;
import android.hardware.*;
import android.app.Activity;
import android.content.DialogInterface;
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.widget.Button;

public class MainActivity extends Activity
{

Button mainButton1;

private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;

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

mainButton1 = (Button) findViewById(R.id.mainButton1);

hasFlash = getApplicationContext().getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

if (!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Sorry");
alert.setMessage("This device doesn't have flash a flash light");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which){
finish();
}
});
alert.show();
return;
}

getCamera();
mainButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
if (isFlashOn){
turnOffFlash();}
else {
turnOnFlash();
}

};

});

}

private void getCamera(){
if (camera == null){
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Failed to open camera", e.getMessage());
}

}
}
private void turnOnFlash() {
if (!isFlashOn){
if (camera == null || params == null){
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
}

private void turnOffFlash() {
if (isFlashOn){
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
}
}

@Override
protected void onDestroy()
{
super.onDestroy();
}

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

turnOffFlash();
}

@Override
protected void onRestart()
{
super.onRestart();
}
@Override
protected void onResume()
{
super.onResume();
if(hasFlash)
turnOnFlash();
}

@Override
protected void onStart()
{
super.onStart();
getCamera();
}

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

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

感谢您的阅读:)

最佳答案

如果您正在使用新设备,则您遵循的代码将无法在较新的设备上运行,我通过遵循此代码使其在 Lollipop 设备上运行,请尝试一下

mCam = Camera.open();
Camera.Parameters p = mCam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mPreviewTexture = new SurfaceTexture(0);
try {
mCam.setPreviewTexture(mPreviewTexture);
} catch (IOException ex) {
// Ignore
}
mCam.startPreview();

关于java - 手电筒无法打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com