gpt4 book ai didi

android - torch 应用程序问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:19 25 4
gpt4 key购买 nike

更新:我已经编辑了我的问题以包含工作代码。我已经解决了我的问题。请参阅我的最新/最后一篇文章。

更新 2:代码已于 2018-06-08 更新。这是 repo 协议(protocol):

https://github.com/amboxer21/FlashLightApp

注意:我使用 ant 从 Linux 命令行编译。

我有一个手电筒应用程序,我正在尝试开始工作,但我运气不好。任何人都可以指出我所缺少的并进行解释吗?我真的很感激!

我可以毫无问题地编译。我可以毫无问题地插入。我可以毫无问题地切换开和关按钮。我可以通过我放置的 toast 看到这一点。基本上所有功能似乎都能正确调用,但后置摄像头上的 LED 灯只是不亮。我在 5.1.1 上使用 Nexus 6 并在 Linux 上开发。此外,我认为这里值得一提,但我能够在我的 Linux 机器和 torch 炒锅上编译第 3 方 torch 应用程序。

主要 Activity :

package com.flash.light;

import android.util.Log;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Handler;
import android.os.Messenger;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.MenuInflater;
import android.view.SurfaceHolder;
import android.view.View.OnClickListener;

import android.widget.Toast;
import android.widget.Button;
import android.widget.ToggleButton;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import android.content.Intent;
import android.content.Context;
import android.content.ComponentName;
import android.content.ServiceConnection;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.support.v7.view.ActionMode;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.app.AppCompatCallback;

public class FlashLight extends Activity implements SurfaceHolder.Callback, AppCompatCallback {

private static final String TAG = "FlashLight FlashLight";

private Camera mCam;
private Parameters params;
private ToggleButton flashLight;
private SurfaceView surfaceView;
private Messenger mService = null;
private SurfaceHolder surfaceHolder;

private boolean mBound;
private boolean hasCameraFlash;
private boolean isBound = false;
private boolean isFlashOn = false;
private static long backPressedTime = 0;

private static Message mtn;
private static Message msg;
private static AppCompatDelegate delegate;
private static ComponentName componentName;
private static PackageManager packageManager;

public void isServiceBound() {
isBound = getApplicationContext().bindService(new Intent(getApplicationContext(),
FlashLightService.class), mConnection, Context.BIND_AUTO_CREATE );
if(isBound) {
getApplicationContext().unbindService(mConnection);
}
}

public void toast(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}

private ServiceConnection mConnection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
mBound = false;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = new Messenger(service);
mBound = true;
}
};

@Override
public void onBackPressed() {
long mTime = System.currentTimeMillis();
if(mTime - backPressedTime > 2000) {
backPressedTime = mTime;
Toast.makeText(this, "Press back again to close app.", Toast.LENGTH_SHORT).show();
}
else {
finish();
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}

private AppCompatDelegate getDelegate() {
if (delegate == null) {
delegate = AppCompatDelegate.create(this, this);
}

return delegate;
}

public boolean supportRequestWindowFeature(int featureId) {
return getDelegate().requestWindowFeature(featureId);
}

public void invalidateOptionsMenu() {
getDelegate().invalidateOptionsMenu();
}

@Override
public void onSupportActionModeStarted(ActionMode mode) { }

@Override
public void onSupportActionModeFinished(ActionMode mode) { }

public ActionMode startSupportActionMode(ActionMode.Callback callback) {
return getDelegate().startSupportActionMode(callback);
}

@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.configureMenu:
Intent configureIntent = new Intent(getApplicationContext(), Configure.class);
startActivityForResult(configureIntent, 0);
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void onDestroy() {
super.onDestroy();
try {
isServiceBound();
if(mCam != null) {
mCam.stopPreview();
mCam.release();
}
}
catch(Exception e) {
e.printStackTrace();
}
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("isFlashOn", isFlashOn);
super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
isFlashOn = savedInstanceState.getBoolean("isFlashOn");
}

private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

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

if(!isMyServiceRunning(FlashLightService.class)) {
Intent serviceIntent = new Intent(getApplicationContext(), FlashLightService.class);
startService(serviceIntent);
getApplicationContext().bindService(new Intent(getApplicationContext(), FlashLightService.class), mConnection,
Context.BIND_AUTO_CREATE);
}

delegate = AppCompatDelegate.create(this, this);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.main);

Toolbar toolbar = (Toolbar) findViewById(R.id.action_toolbar);

delegate.setSupportActionBar(toolbar);
delegate.getSupportActionBar().setDisplayShowTitleEnabled(true);

if(savedInstanceState != null) {
isFlashOn = savedInstanceState.getBoolean("isFlashOn");
}

surfaceView = (SurfaceView)findViewById(R.id.preview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(FlashLight.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

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

if(!(hasCameraFlash)) {
toast("Camera does not have flash feature.");
return;
}
else {
getCamera();
}

flashLight = (ToggleButton)findViewById(R.id.flashLight);
flashLight.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
try {
if(!(isFlashOn)) {
params = mCam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCam.setParameters(params);
mCam.startPreview();
isFlashOn = true;
}
else {
params = mCam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
mCam.setParameters(params);
mCam.stopPreview();
isFlashOn = false;
}
}
catch(Exception e) {
e.printStackTrace();
}
}
});
}

public void getCamera() throws NullPointerException {
try {
if(mCam == null) {
mCam = Camera.open();
}
}
catch(NullPointerException e) {
e.printStackTrace();
}
}

public void surfaceCreated(SurfaceHolder holder) {
try {
mCam.setPreviewDisplay(holder);
}
catch(Exception e) {
e.printStackTrace();
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

public void surfaceDestroyed(SurfaceHolder holder) { }

}

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#E8E8E8"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<android.support.v7.widget.Toolbar
android:background="#3F51B5"
android:id="@+id/action_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:label="@string/app_name">
</android.support.v7.widget.Toolbar>

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

<ToggleButton
android:textOn=""
android:textOff=""
android:id="@+id/flashLight"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="100dp"
android:background="@drawable/check"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

list :

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

<!-- PERMISSIONS -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>

<!-- FEATURES -->
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.touchscreen"/>
<uses-feature android:name="android.hardware.camera.flash"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>

<!-- TARGET SDK VERSION -->
<uses-sdk android:minSdkVersion="21"/>
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<activity android:name="FlashLight"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".FlashLightService" android:enabled="true"/>
</application>
</manifest>

最佳答案

原来我需要一个 surfaceHolder 和 surfaceView 才能调用 startPreview 方法。

这是我通过 github 运行的 torch 应用程序 -> https://github.com/amboxer21/FlashLightApp

关于android - torch 应用程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36115579/

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