火炬应用程序问题

Posted

技术标签:

【中文标题】火炬应用程序问题【英文标题】:Torch app trouble 【发布时间】:2016-07-07 01:11:01 【问题描述】:

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

UPDATE2:代码已于 2018-06-08 更新。这是回购:

https://github.com/amboxer21/FlashLightApp

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

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

我可以毫无问题地编译。我可以毫无问题地推动。我可以毫无问题地切换打开和关闭按钮。我可以从我放置的祝酒词中看到这一点。基本上所有功能似乎都被正确调用,但后置摄像头上的 LED 灯不亮。我在 5.1.1 上使用 Nexus 6 并在 Linux 上开发。我也认为这里值得一提,但我可以在我的 Linux 机器和火炬炒锅上编译第 3 方火炬应用程序。

主要活动:

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)   


main.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_
  android:layout_>

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

  <SurfaceView
    android:id="@+id/preview"
    android:layout_
    android:layout_/>

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

</RelativeLayout>

清单:

<?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>

【问题讨论】:

检查闪光灯是否可用? context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 不确定这是问题所在,因为我可以使用市场上的 Torch 应用程序。我什至从 git 编译了一个 3rd 方应用程序,并且火炬工作了。 好的,我建议您也提及您用于测试的设备的名称,一些三星设备存在一些已知问题。在此处查看更多信息:***.com/questions/6068803/… 对不起,它是 5.1.1 上的 nexus 6 【参考方案1】:

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

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

【讨论】:

以上是关于火炬应用程序问题的主要内容,如果未能解决你的问题,请参考以下文章

火炬:如何在另一个线程中注册一个火炬回调?

火炬之光怎么修改成中文?

火炬之光2镜像prophet怎么安装

Spyder 抱怨找不到“火炬”模块

火炬代码不在 GPU 上运行

在某个索引处连接火炬张量