android - android使用Camera2以编程方式打开/关闭相机闪光灯

133 2

我正在做一个手电筒的简单应用程序。我需要使用Camera2 API打开和关闭闪光灯,因为我的旧camera API出错了。

代码:


Camera cam = Camera.open(); 


Parameters p = cam.getParameters();


p.setFlashMode(Parameters.FLASH_MODE_TORCH);


cam.setParameters(p);


cam.startPreview();



时间: 原作者:

128 2

有关CameraManager的文档,这是在API 21以上的相机上激活闪光灯的基本代码。


CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);


String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.


camManager.setTorchMode(cameraId, true);



Android CameraManager文档

Android.hardware.camera2文档

非常重要! 你需要使用try/catch丢弃可能的错误,并确保没有其他高优先级应用程序。

原作者:
102 5

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


 mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);


 try {


 for (String camID : mCameraManager.getCameraIdList()) {


 CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(camID);


 int lensFacing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);


 if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {


 mCameraId = camID;


 break;


 } else if (lensFacing == CameraCharacteristics.LENS_FACING_BACK && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {


 mCameraId = camID;


 }


 }


 if (mCameraId != null) {


 mCameraManager.setTorchMode(mCameraId, true);


 }


 } catch (CameraAccessException e) {


 e.printStackTrace();


 }


}



mCameraId最好优先打开前摄像头闪光灯,如果没有前闪光灯,则使用后摄像头闪光灯,如果设备没有闪光灯,那么mCameraId为空,并且在上面的代码中不会调用setTorchMode 。

原作者:
54 2


private void Flash() {


 if(cameraDevice == null)


 return;


 CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);


 try {


 if(flashSwitch == 0 ) {


 camManager.setTorchMode(cameraId, true);


 Toast.makeText(MainActivity.this,"Flash Turn On", Toast.LENGTH_SHORT).show();


 flashSwitch = 1;


 }


 else{


 camManager.setTorchMode(cameraId, false);


 Toast.makeText(MainActivity.this,"Flash Turn Off", Toast.LENGTH_SHORT).show();


 flashSwitch = 0;


 }


 } catch (CameraAccessException e) {


 e.printStackTrace();


 }


 }



原作者:
122 3

最简单和最普通的做手电筒应用的方法

// xml代码


<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"


 xmlns:tools="http://schemas.android.com/tools"


 android:layout_width="match_parent"


 android:layout_height="match_parent"


 android:paddingBottom="@dimen/activity_vertical_margin"


 android:paddingLeft="@dimen/activity_horizontal_margin"


 android:paddingRight="@dimen/activity_horizontal_margin"


 android:paddingTop="@dimen/activity_vertical_margin"


 tools:context="com.example.android.torcia.MainActivity">



 <Button


 android:layout_width="wrap_content"


 android:layout_height="wrap_content"


 android:text="ON"


 android:id="@+id/flash_button"


 android:layout_centerVertical="true"


 android:layout_centerHorizontal="true"


 android:onClick="turnOnOff"/>


</RelativeLayout>



//java代码


public class MainActivity extends AppCompatActivity {


 @Override


 protected void onCreate(Bundle savedInstanceState) {


 super.onCreate(savedInstanceState);


 setContentView(R.layout.activity_main);


 }



 Button flashButton;


 String buttonText;



 public void checkState(String buttonText) {



 switch (buttonText) {



 case"ON":



 flashButton.setText("OFF");


 flashOn();



 break;



 case"OFF":



 flashButton.setText("ON");


 flashOff();



 break;


 }


 }



 public void turnOnOff(View view) {



 flashButton = (Button) findViewById(R.id.flash_button);


 buttonText = flashButton.getText().toString();


 checkState(this.buttonText);



 }



 public void flashOn() {



 Camera camera = Camera.open();


 Camera.Parameters parameters = camera.getParameters();


 parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);


 camera.setParameters(parameters);


 camera.startPreview();



 }



 public void flashOff() {



 Camera camera2 = Camera.open();


 Camera.Parameters parameters2 = camera2.getParameters();


 parameters2.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);


 camera2.setParameters(parameters2);


 camera2.stopPreview();



 }



}



// AndroidManifest


<?xml version="1.0" encoding="utf-8"?>




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


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



<application


 android:allowBackup="true"


 android:icon="@mipmap/ic_launcher"


 android:label="@string/app_name"


 android:supportsRtl="true"


 android:theme="@style/AppTheme">



 <activity android:name=".MainActivity">


 <intent-filter>


 <action android:name="android.intent.action.MAIN" />



 <category android:name="android.intent.category.LAUNCHER" />


 </intent-filter>


 </activity>


</application>



原作者:
...