gpt4 book ai didi

java - Nexus 5 上的 Android 手电筒

转载 作者:太空狗 更新时间:2023-10-29 16:38:16 32 4
gpt4 key购买 nike

我正在尝试开发一个应用程序,用于在点击切换按钮时打开手电筒。我尝试了我在网上找到的所有方法,但在我的 nexus 5 中没有成功。唯一可用的应用程序是 cyanogenmod torch,但即使我采用该代码并将其写入我的应用程序,它也会创建一个通知,如果单击它会打开cyanogenmod torch 应用程序,而不是我的!这是个问题!!有没有可能有一个简单的代码也适用于 nexus 5?

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

public void turnOnFlashLight() {
try {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
cam = Camera.open();
cam.setPreviewTexture(new SurfaceTexture(0));
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);

cam.setParameters(p);
cam.startPreview();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception throws in turning on flashlight.", Toast.LENGTH_SHORT).show();
}
}

编辑:logcat

04-09 21:52:21.967: E/AndroidRuntime(5346): FATAL EXCEPTION: main
04-09 21:52:21.967: E/AndroidRuntime(5346): Process: com.lob.twixlight, PID: 5346
04-09 21:52:21.967: E/AndroidRuntime(5346): android.util.SuperNotCalledException: Activity {com.lob.twixlight/com.lob.twixlight.MainActivityTwixLight} did not call through to super.onStart()
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.Activity.performStart(Activity.java:5243)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.access$800(ActivityThread.java:145)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.os.Looper.loop(Looper.java:136)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.main(ActivityThread.java:5081)
04-09 21:52:21.967: E/AndroidRuntime(5346): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:52:21.967: E/AndroidRuntime(5346): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 21:52:21.967: E/AndroidRuntime(5346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-09 21:52:21.967: E/AndroidRuntime(5346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-09 21:52:21.967: E/AndroidRuntime(5346): at dalvik.system.NativeStart.main(Native Method)

整个主要 Activity

public class MainActivity extends Activity implements Callback  { 


private ImageView imageView3;
private ImageButton mButtonOn;
private Camera cam;
private SurfaceHolder mHolder ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
new AlertDialog.Builder(this);
setContentView(R.layout.activity_main);
mButtonOn = (ImageButton) findViewById(R.id.my_button);
imageView3 = (ImageView) findViewById(R.id.imageView3);


SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
mHolder = preview.getHolder();
mHolder.addCallback(this);
}

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

public void info(View v) {
Intent intent = new Intent(this, FullscreenActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in, R.anim.out);
}

protected void onStart() {
super.onStart ();
SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
mHolder = preview.getHolder();
mHolder.addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mHolder = holder;

try {
Log.i("SurfaceHolder", "setting preview");
cam.setPreviewDisplay(mHolder);
} catch (IOException e){
e.printStackTrace();
}
}

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

mHolder = null;
}


}

最佳答案

好的,首先,确保您已经将表面 View 添加到您的 xml 布局中,在您的按钮之上:

<SurfaceView
android:id="@+id/PREVIEW"
android:layout_width="1dp"
android:layout_height="1dp"/>

//image button comes below

<ImageButton
android:id="@+id/my_button" />

现在,在您的 java 类中,实现 SurfaceHolder.Callback 接口(interface),例如:

public class MyFlashlight extends Activity implements Callback

这将自动生成三个回调方法:

surfaceChanged();
surfaceCreated();
surfaceDestroyed();

暂时保持原样。在你的 onStart();方法,添加以下内容:

SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);

不要将它添加到 onCreate();因为它(onCreate();)只被调用一次,如果要重新启动 Activity ,它将导致问题。

在你的 turnOnFlash();方法,照常做:

//get parameters
//set parameters
//set flashmode
//start preview

在你的 surfaceCreated();方法,添加这个:

mHolder = holder;
try {
Log.i("SurfaceHolder", "setting preview");
camera.setPreviewDisplay(mHolder);
} catch (IOException e){
e.printStackTrace();
}

在你的 surfaceDestroyed();方法,停止预览:

camera.stopPreview();
mHolder = null;

离开surfaceChanged();空

就是这样!!!!试试吧!

这是一个完整的工作示例:

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 MyFlashLight extends Activity implements Callback{

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

private Camera camera;

private Button button;

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

SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}
}

@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) {
turnOffFlash();
} else {
turnOnFlash();
}
}
});


private void turnOnFlash(){
Log.i("info", "torch is turn on!");

p.setFlashMode(Parameters.FLASH_MODE_TORCH);

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

}
private void turnOffFlash(){
Log.i("info", "torch is turn off!");

p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){

}

@Override
public void surfaceCreated(SurfaceHolder holder){
mHolder = holder;
try {
Log.i("SurfaceHolder", "setting preview");
camera.setPreviewDisplay(mHolder);
} catch (IOException e){
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
Log.i("SurfaceHolder", "stopping preview");
camera.stopPreview();
mHolder = null;
}

}

关于java - Nexus 5 上的 Android 手电筒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22855494/

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