2016-03-20 59 views
1

更新:我編輯了我的問題以包含工作代碼。我已經解決了我的問題。查看我最近的/最後一篇文章。火炬應用程序問題

注意:我從Linux命令行進行編譯。

我有一個火炬應用程序,我正在努力工作,但我沒有運氣。任何人都可以指出我缺少的東西並解釋嗎?我真的很感激!

我可以編譯沒有問題。我可以推動沒有問題。我可以切換開啓和關閉按鈕,沒有問題。我可以通過我已經設置的敬酒來看到這一點。基本上所有的功能似乎都可以正確調用,但後置攝像頭上的LED燈不亮。我在5.1.1上使用Nexus 6並在Linux上開發。另外我在這裏提到它值得一提,但是我可以在我的Linux機器和火把上編寫第三方的火炬應用程序。

主要活動:

package com.flash.light; 

import android.util.Log; 
import android.os.Bundle; 
import android.view.View; 
import android.app.Activity; 
import android.widget.Toast; 
import android.widget.Button; 
import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceView; 
import android.view.SurfaceHolder; 
import android.view.View.OnClickListener; 
import android.content.pm.PackageManager; 
import android.hardware.Camera.Parameters; 

public class FlashLight extends Activity implements SurfaceHolder.Callback { 

    private Camera mCam; 
    private boolean isFlashOn; 
    private Parameters params; 
    private Button flashLight; 
    private boolean hasCameraFlash; 
    private SurfaceView surfaceView; 
    private SurfaceHolder surfaceHolder; 

    private static final String TAG = FlashLight.class.getSimpleName(); 

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

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

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

    if(!(hasCameraFlash)) { 
     Toast.makeText(this, "Camera does not have flash feature.", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    else { 
     getCamera(); 
     Toast.makeText(this, "Opened Camera.", Toast.LENGTH_LONG).show(); 
    } 

    isFlashOn = false; 
    flashLight = (Button)findViewById(R.id.flashLight); 
    flashLight.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 
     try { 
      toggle(); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
     } 

    }); 
    } 

    public void toggle() { 
    try { 
     if(!(isFlashOn)) { 
     flashLightOn(); 
     flashLight.setText("OFF"); 
     Toast.makeText(this, "flashLightOn()", Toast.LENGTH_LONG).show(); 
     } 
     else { 
     flashLightOff(); 
     flashLight.setText("ON"); 
     Toast.makeText(this, "flashLightOff()", Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public void getCamera() { 
     try { 
     mCam = Camera.open(); 
     params = mCam.getParameters(); 
     } 
     catch(Exception e) { 
     e.printStackTrace(); 
     } 
    } 

    public void flashLightOn() { 
    if(mCam == null) { 
     Toast.makeText(this, "Camera not found.", Toast.LENGTH_LONG).show(); 
     return; 
    } 

    if(!(isFlashOn)) { 
     params = mCam.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
     mCam.setParameters(params); 
     mCam.startPreview(); 
     isFlashOn = true; 
     Toast.makeText(this, "isFlashOn = true", Toast.LENGTH_LONG).show(); 
    } 
    } 

    public void flashLightOff() { 
    try { 
     if(isFlashOn) { 
     params = mCam.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_OFF); 
     mCam.setParameters(params); 
     mCam.stopPreview(); 
     mCam.release(); 
     isFlashOn = false; 
     Toast.makeText(this, "isFlashOn = false", Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    Log.d(TAG,"surfaceChanged()"); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
    try { 
     mCam.setPreviewDisplay(holder); 
     Toast.makeText(this, "Setting preview.", Toast.LENGTH_LONG).show(); 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
    Toast.makeText(this, "Preview stopped.", Toast.LENGTH_LONG).show(); 
    mCam.stopPreview(); 
    } 

} 

main.xml中:

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

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="FlashLight"/> 

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

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="ON" 
    android:id="@+id/flashLight"/> 

</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"> 

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

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 
     <activity android:name="FlashLight" 
        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> 
+0

檢查閃光燈是否可用呢? 'context.getPackageManager()。hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);' – AADProgramming

+0

由於我能夠使用來自市場的火炬應用程序,因此不能確定這個問題。我甚至從git編譯了第三方應用程序,並且火炬工作正常。 – Aguevara

+0

好吧,我建議你也提一下你用來測試的設備的名稱,還有一些關於某些三星設備的已知問題。點擊此處查看:http://stackoverflow.com/questions/6068803/how-to-turn-on-camera-flash-light-programmatically-in-android – AADProgramming

回答