當在天氣晴朗時,光線充足,相機拍攝是可以不用打開閃燈,但在光線不充足環境,便要打開相機閃燈(Flash),作為補光之用,而且手機閃燈也可以作為手電筒(Torch),非常方便,特別是步行在黑暗的路上作照明用途。
Android 相機閃燈程式(Camera Flash Program) |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11g Camera Flash Bugworkshop
- 程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11g Camera Flash Bugworkshop
- 測試環境:Vivo(維沃)X5Max Android 4.4.4(KitKat)版本
Android相機閃燈程式(Camera Flash Program)編程步驟:
- 設定 Camera 參數
- 設置 layout ScrollView 參數 - activity_camera.xml
- 設置手機閃燈程式
AndroidManifest.xml 獲取 Camera 許可權:
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT"/> |
activity_camera.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0" />
<Button android:id="@+id/btn_flash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:text="Camera Flash" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java:
package bugworkshop.blogspot.com.camera;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.hardware.Camera; import java.util.List;
public class MainActivity extends AppCompatActivity { private TextView mTv_camea; private Button mBtn_flash; private boolean flash_on = false; private Camera mCamera;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTv_camea = (TextView) findViewById(R.id.textView); mBtn_flash = (Button) findViewById(R.id.btn_flash);
mCamera = Camera.open();
mBtn_flash.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!flash_on) { setFlashLight(true); flash_on = true; } else { setFlashLight(false); flash_on = false; } } }); }
public boolean setFlashLight(boolean open) { if (mCamera == null) { return false; } Camera.Parameters parameters = mCamera.getParameters(); if (parameters == null) { return false; } List<String> flashModes = parameters.getSupportedFlashModes(); // Check if camera flash exists if (null == flashModes || 0 == flashModes.size()) { // Use the screen as a flashlight (next best thing) return false; }
String flashMode = parameters.getFlashMode(); if (open) { if (Camera.Parameters.FLASH_MODE_TORCH.equals(flashMode)) { return true; } // Turn on the flash if (flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) { parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); mCamera.setParameters(parameters); return true; } else { return false; } } else { if (Camera.Parameters.FLASH_MODE_OFF.equals(flashMode)) { return true; } // Turn on the flash if (flashModes.contains(Camera.Parameters.FLASH_MODE_OFF)) { parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); mCamera.setParameters(parameters); return true; } else return false; } } } |
沒有留言:
張貼留言