implement Flash
This commit is contained in:
parent
c518884d00
commit
9504fe9ef8
|
@ -8,6 +8,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import butterknife.Bind;
|
||||
|
@ -17,9 +18,11 @@ import butterknife.OnClick;
|
|||
public class MainActivity extends AppCompatActivity {
|
||||
@Bind(R.id.viewHolder) RelativeLayout viewHolder;
|
||||
@Bind(R.id.toggle_camera) View toggleCameraBtn;
|
||||
@Bind(R.id.toggle_flash) ImageView toggleFlashBtn;
|
||||
|
||||
private Preview preview;
|
||||
private int currCamera;
|
||||
private boolean isFlashEnabled;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -37,15 +40,33 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
@OnClick(R.id.toggle_camera)
|
||||
public void toggleCamera() {
|
||||
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK)
|
||||
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
|
||||
currCamera = Camera.CameraInfo.CAMERA_FACING_FRONT;
|
||||
else
|
||||
} else {
|
||||
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
|
||||
}
|
||||
|
||||
disableFlash();
|
||||
preview.releaseCamera();
|
||||
preview.setCamera(currCamera);
|
||||
}
|
||||
|
||||
@OnClick(R.id.toggle_flash)
|
||||
public void toggleFlash() {
|
||||
if (isFlashEnabled) {
|
||||
disableFlash();
|
||||
} else if (preview.enableFlash()) {
|
||||
isFlashEnabled = preview.enableFlash();
|
||||
toggleFlashBtn.setImageResource(R.mipmap.flash_on);
|
||||
}
|
||||
}
|
||||
|
||||
private void disableFlash() {
|
||||
preview.disableFlash();
|
||||
isFlashEnabled = false;
|
||||
toggleFlashBtn.setImageResource(R.mipmap.flash_off);
|
||||
}
|
||||
|
||||
@OnClick(R.id.shutter)
|
||||
public void takePicture() {
|
||||
preview.takePicture();
|
||||
|
|
|
@ -30,6 +30,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
private static boolean canTakePicture;
|
||||
private static Activity activity;
|
||||
private static int currCameraId;
|
||||
private static boolean isFlashEnabled;
|
||||
private static Camera.Parameters parameters;
|
||||
|
||||
public Preview(Context context) {
|
||||
super(context);
|
||||
|
@ -66,15 +68,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
camera = newCamera;
|
||||
|
||||
if (camera != null) {
|
||||
supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes();
|
||||
parameters = camera.getParameters();
|
||||
supportedPreviewSizes = parameters.getSupportedPreviewSizes();
|
||||
requestLayout();
|
||||
|
||||
final Camera.Parameters params = camera.getParameters();
|
||||
final List<String> focusModes = params.getSupportedFocusModes();
|
||||
final List<String> focusModes = parameters.getSupportedFocusModes();
|
||||
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
|
||||
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
|
||||
|
||||
camera.setParameters(params);
|
||||
camera.setParameters(parameters);
|
||||
setCameraDisplayOrientation(cameraId, camera);
|
||||
|
||||
if (canTakePicture) {
|
||||
|
@ -119,6 +121,10 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
|
||||
public void takePicture() {
|
||||
if (canTakePicture) {
|
||||
if (isFlashEnabled) {
|
||||
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
|
||||
camera.setParameters(parameters);
|
||||
}
|
||||
camera.takePicture(null, null, takePictureCallback);
|
||||
}
|
||||
canTakePicture = false;
|
||||
|
@ -139,6 +145,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
|
||||
final Camera.CameraInfo info = Utils.getCameraInfo(currCameraId);
|
||||
new PhotoProcessor(getContext(), info.facing).execute(data);
|
||||
|
||||
if (isFlashEnabled) {
|
||||
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||
camera.setParameters(parameters);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -148,7 +159,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
|
||||
camera.cancelAutoFocus();
|
||||
final Rect focusRect = calculateFocusArea(event.getX(), event.getY());
|
||||
final Camera.Parameters parameters = camera.getParameters();
|
||||
if (parameters.getMaxNumFocusAreas() > 0) {
|
||||
final List<Camera.Area> focusAreas = new ArrayList<>(1);
|
||||
focusAreas.add(new Camera.Area(focusRect, 1000));
|
||||
|
@ -211,7 +221,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
private void setupPreview() {
|
||||
canTakePicture = true;
|
||||
if (camera != null && previewSize != null) {
|
||||
final Camera.Parameters parameters = camera.getParameters();
|
||||
parameters.setPreviewSize(previewSize.width, previewSize.height);
|
||||
|
||||
requestLayout();
|
||||
|
@ -280,4 +289,17 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||
focusArea(event);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean enableFlash() {
|
||||
if (!Utils.hasFlash(camera)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
isFlashEnabled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void disableFlash() {
|
||||
isFlashEnabled = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import android.content.Context;
|
|||
import android.hardware.Camera;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Utils {
|
||||
public static Camera.CameraInfo getCameraInfo(int cameraId) {
|
||||
final Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
|
||||
|
@ -14,4 +16,24 @@ public class Utils {
|
|||
public static void showToast(Context context, int resId) {
|
||||
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public static boolean hasFlash(Camera camera) {
|
||||
if (camera == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Camera.Parameters parameters = camera.getParameters();
|
||||
|
||||
if (parameters.getFlashMode() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final List<String> supportedFlashModes = parameters.getSupportedFlashModes();
|
||||
if (supportedFlashModes == null || supportedFlashModes.isEmpty() ||
|
||||
supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,8 +37,7 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:src="@mipmap/ic_launcher"
|
||||
android:visibility="invisible"/>
|
||||
android:src="@mipmap/flash_off"/>
|
||||
|
||||
</LinearLayout>
|
||||
</RelativeLayout>
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
Loading…
Reference in New Issue