mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-06-27 09:02:59 +02:00
implement Flash
This commit is contained in:
@ -8,6 +8,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
|
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
@ -17,9 +18,11 @@ import butterknife.OnClick;
|
|||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
@Bind(R.id.viewHolder) RelativeLayout viewHolder;
|
@Bind(R.id.viewHolder) RelativeLayout viewHolder;
|
||||||
@Bind(R.id.toggle_camera) View toggleCameraBtn;
|
@Bind(R.id.toggle_camera) View toggleCameraBtn;
|
||||||
|
@Bind(R.id.toggle_flash) ImageView toggleFlashBtn;
|
||||||
|
|
||||||
private Preview preview;
|
private Preview preview;
|
||||||
private int currCamera;
|
private int currCamera;
|
||||||
|
private boolean isFlashEnabled;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -37,15 +40,33 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
@OnClick(R.id.toggle_camera)
|
@OnClick(R.id.toggle_camera)
|
||||||
public void toggleCamera() {
|
public void toggleCamera() {
|
||||||
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK)
|
if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) {
|
||||||
currCamera = Camera.CameraInfo.CAMERA_FACING_FRONT;
|
currCamera = Camera.CameraInfo.CAMERA_FACING_FRONT;
|
||||||
else
|
} else {
|
||||||
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
|
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
disableFlash();
|
||||||
preview.releaseCamera();
|
preview.releaseCamera();
|
||||||
preview.setCamera(currCamera);
|
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)
|
@OnClick(R.id.shutter)
|
||||||
public void takePicture() {
|
public void takePicture() {
|
||||||
preview.takePicture();
|
preview.takePicture();
|
||||||
|
@ -30,6 +30,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
private static boolean canTakePicture;
|
private static boolean canTakePicture;
|
||||||
private static Activity activity;
|
private static Activity activity;
|
||||||
private static int currCameraId;
|
private static int currCameraId;
|
||||||
|
private static boolean isFlashEnabled;
|
||||||
|
private static Camera.Parameters parameters;
|
||||||
|
|
||||||
public Preview(Context context) {
|
public Preview(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
@ -66,15 +68,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
camera = newCamera;
|
camera = newCamera;
|
||||||
|
|
||||||
if (camera != null) {
|
if (camera != null) {
|
||||||
supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes();
|
parameters = camera.getParameters();
|
||||||
|
supportedPreviewSizes = parameters.getSupportedPreviewSizes();
|
||||||
requestLayout();
|
requestLayout();
|
||||||
|
|
||||||
final Camera.Parameters params = camera.getParameters();
|
final List<String> focusModes = parameters.getSupportedFocusModes();
|
||||||
final List<String> focusModes = params.getSupportedFocusModes();
|
|
||||||
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
|
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);
|
setCameraDisplayOrientation(cameraId, camera);
|
||||||
|
|
||||||
if (canTakePicture) {
|
if (canTakePicture) {
|
||||||
@ -119,6 +121,10 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
|
|
||||||
public void takePicture() {
|
public void takePicture() {
|
||||||
if (canTakePicture) {
|
if (canTakePicture) {
|
||||||
|
if (isFlashEnabled) {
|
||||||
|
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
|
||||||
|
camera.setParameters(parameters);
|
||||||
|
}
|
||||||
camera.takePicture(null, null, takePictureCallback);
|
camera.takePicture(null, null, takePictureCallback);
|
||||||
}
|
}
|
||||||
canTakePicture = false;
|
canTakePicture = false;
|
||||||
@ -139,6 +145,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
|
|
||||||
final Camera.CameraInfo info = Utils.getCameraInfo(currCameraId);
|
final Camera.CameraInfo info = Utils.getCameraInfo(currCameraId);
|
||||||
new PhotoProcessor(getContext(), info.facing).execute(data);
|
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();
|
camera.cancelAutoFocus();
|
||||||
final Rect focusRect = calculateFocusArea(event.getX(), event.getY());
|
final Rect focusRect = calculateFocusArea(event.getX(), event.getY());
|
||||||
final Camera.Parameters parameters = camera.getParameters();
|
|
||||||
if (parameters.getMaxNumFocusAreas() > 0) {
|
if (parameters.getMaxNumFocusAreas() > 0) {
|
||||||
final List<Camera.Area> focusAreas = new ArrayList<>(1);
|
final List<Camera.Area> focusAreas = new ArrayList<>(1);
|
||||||
focusAreas.add(new Camera.Area(focusRect, 1000));
|
focusAreas.add(new Camera.Area(focusRect, 1000));
|
||||||
@ -211,7 +221,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
private void setupPreview() {
|
private void setupPreview() {
|
||||||
canTakePicture = true;
|
canTakePicture = true;
|
||||||
if (camera != null && previewSize != null) {
|
if (camera != null && previewSize != null) {
|
||||||
final Camera.Parameters parameters = camera.getParameters();
|
|
||||||
parameters.setPreviewSize(previewSize.width, previewSize.height);
|
parameters.setPreviewSize(previewSize.width, previewSize.height);
|
||||||
|
|
||||||
requestLayout();
|
requestLayout();
|
||||||
@ -280,4 +289,17 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
focusArea(event);
|
focusArea(event);
|
||||||
return false;
|
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.hardware.Camera;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
public static Camera.CameraInfo getCameraInfo(int cameraId) {
|
public static Camera.CameraInfo getCameraInfo(int cameraId) {
|
||||||
final Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
|
final Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
|
||||||
@ -14,4 +16,24 @@ public class Utils {
|
|||||||
public static void showToast(Context context, int resId) {
|
public static void showToast(Context context, int resId) {
|
||||||
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
|
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_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:src="@mipmap/ic_launcher"
|
android:src="@mipmap/flash_off"/>
|
||||||
android:visibility="invisible"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
BIN
app/src/main/res/mipmap-hdpi/flash_off.png
Normal file
BIN
app/src/main/res/mipmap-hdpi/flash_off.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
BIN
app/src/main/res/mipmap-hdpi/flash_on.png
Normal file
BIN
app/src/main/res/mipmap-hdpi/flash_on.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
Reference in New Issue
Block a user