You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
8.4 KiB
226 lines
8.4 KiB
package com.otaliastudios.cameraview.demo;
|
|
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import androidx.annotation.NonNull;
|
|
import com.google.android.material.bottomsheet.BottomSheetBehavior;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.ViewTreeObserver;
|
|
import android.widget.Toast;
|
|
|
|
import com.otaliastudios.cameraview.CameraException;
|
|
import com.otaliastudios.cameraview.CameraListener;
|
|
import com.otaliastudios.cameraview.CameraLogger;
|
|
import com.otaliastudios.cameraview.CameraOptions;
|
|
import com.otaliastudios.cameraview.CameraView;
|
|
import com.otaliastudios.cameraview.PictureResult;
|
|
import com.otaliastudios.cameraview.Mode;
|
|
import com.otaliastudios.cameraview.VideoResult;
|
|
|
|
import java.io.File;
|
|
|
|
|
|
public class CameraActivity extends AppCompatActivity implements View.OnClickListener, ControlView.Callback {
|
|
|
|
private CameraView camera;
|
|
private ViewGroup controlPanel;
|
|
|
|
// To show stuff in the callback
|
|
private long mCaptureTime;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_camera);
|
|
CameraLogger.setLogLevel(CameraLogger.LEVEL_VERBOSE);
|
|
|
|
camera = findViewById(R.id.camera);
|
|
camera.setLifecycleOwner(this);
|
|
camera.addCameraListener(new CameraListener() {
|
|
public void onCameraOpened(@NonNull CameraOptions options) { onOpened(options); }
|
|
public void onPictureTaken(@NonNull PictureResult result) { onPicture(result); }
|
|
public void onVideoTaken(@NonNull VideoResult result) { onVideo(result); }
|
|
public void onCameraError(@NonNull CameraException exception) {
|
|
onError(exception);
|
|
}
|
|
});
|
|
|
|
findViewById(R.id.edit).setOnClickListener(this);
|
|
findViewById(R.id.capturePicture).setOnClickListener(this);
|
|
findViewById(R.id.capturePictureSnapshot).setOnClickListener(this);
|
|
findViewById(R.id.captureVideo).setOnClickListener(this);
|
|
findViewById(R.id.captureVideoSnapshot).setOnClickListener(this);
|
|
findViewById(R.id.toggleCamera).setOnClickListener(this);
|
|
|
|
controlPanel = findViewById(R.id.controls);
|
|
ViewGroup group = (ViewGroup) controlPanel.getChildAt(0);
|
|
Control[] controls = Control.values();
|
|
for (Control control : controls) {
|
|
ControlView view = new ControlView(this, control, this);
|
|
group.addView(view,
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
}
|
|
|
|
controlPanel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
@Override
|
|
public void onGlobalLayout() {
|
|
BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel);
|
|
b.setState(BottomSheetBehavior.STATE_HIDDEN);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void message(String content, boolean important) {
|
|
int length = important ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
|
|
Toast.makeText(this, content, length).show();
|
|
}
|
|
|
|
private void onOpened(CameraOptions options) {
|
|
ViewGroup group = (ViewGroup) controlPanel.getChildAt(0);
|
|
for (int i = 0; i < group.getChildCount(); i++) {
|
|
ControlView view = (ControlView) group.getChildAt(i);
|
|
view.onCameraOpened(camera, options);
|
|
}
|
|
}
|
|
|
|
private void onError(@NonNull CameraException exception) {
|
|
message("Got CameraException #" + exception.getReason(), true);
|
|
}
|
|
|
|
private void onPicture(PictureResult result) {
|
|
if (camera.isTakingVideo()) {
|
|
message("Captured while taking video. Size=" + result.getSize(), false);
|
|
return;
|
|
}
|
|
|
|
// This can happen if picture was taken with a gesture.
|
|
long callbackTime = System.currentTimeMillis();
|
|
if (mCaptureTime == 0) mCaptureTime = callbackTime - 300;
|
|
PicturePreviewActivity.setPictureResult(result);
|
|
Intent intent = new Intent(CameraActivity.this, PicturePreviewActivity.class);
|
|
intent.putExtra("delay", callbackTime - mCaptureTime);
|
|
startActivity(intent);
|
|
mCaptureTime = 0;
|
|
}
|
|
|
|
private void onVideo(VideoResult video) {
|
|
VideoPreviewActivity.setVideoResult(video);
|
|
Intent intent = new Intent(CameraActivity.this, VideoPreviewActivity.class);
|
|
startActivity(intent);
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View view) {
|
|
switch (view.getId()) {
|
|
case R.id.edit: edit(); break;
|
|
case R.id.capturePicture: capturePicture(); break;
|
|
case R.id.capturePictureSnapshot: capturePictureSnapshot(); break;
|
|
case R.id.captureVideo: captureVideo(); break;
|
|
case R.id.captureVideoSnapshot: captureVideoSnapshot(); break;
|
|
case R.id.toggleCamera: toggleCamera(); break;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onBackPressed() {
|
|
BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel);
|
|
if (b.getState() != BottomSheetBehavior.STATE_HIDDEN) {
|
|
b.setState(BottomSheetBehavior.STATE_HIDDEN);
|
|
return;
|
|
}
|
|
super.onBackPressed();
|
|
}
|
|
|
|
private void edit() {
|
|
BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel);
|
|
b.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
|
}
|
|
|
|
private void capturePicture() {
|
|
if (camera.getMode() == Mode.VIDEO) {
|
|
message("Can't take HQ pictures while in VIDEO mode.", false);
|
|
return;
|
|
}
|
|
if (camera.isTakingPicture()) return;
|
|
mCaptureTime = System.currentTimeMillis();
|
|
message("Capturing picture...", false);
|
|
camera.takePicture();
|
|
}
|
|
|
|
private void capturePictureSnapshot() {
|
|
if (camera.isTakingPicture()) return;
|
|
mCaptureTime = System.currentTimeMillis();
|
|
message("Capturing picture snapshot...", false);
|
|
camera.takePictureSnapshot();
|
|
}
|
|
|
|
private void captureVideo() {
|
|
if (camera.getMode() == Mode.PICTURE) {
|
|
message("Can't record HQ videos while in PICTURE mode.", false);
|
|
return;
|
|
}
|
|
if (camera.isTakingPicture() || camera.isTakingVideo()) return;
|
|
message("Recording for 5 seconds...", true);
|
|
camera.takeVideo(new File(getFilesDir(), "video.mp4"), 5000);
|
|
}
|
|
|
|
private void captureVideoSnapshot() {
|
|
if (camera.isTakingVideo()) {
|
|
message("Already taking video.", false);
|
|
return;
|
|
}
|
|
message("Recording snapshot for 5 seconds...", true);
|
|
camera.takeVideoSnapshot(new File(getFilesDir(), "video.mp4"), 5000);
|
|
}
|
|
|
|
private void toggleCamera() {
|
|
if (camera.isTakingPicture() || camera.isTakingVideo()) return;
|
|
switch (camera.toggleFacing()) {
|
|
case BACK:
|
|
message("Switched to back camera!", false);
|
|
break;
|
|
|
|
case FRONT:
|
|
message("Switched to front camera!", false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onValueChanged(Control control, Object value, String name) {
|
|
if (!camera.isHardwareAccelerated() && (control == Control.WIDTH || control == Control.HEIGHT)) {
|
|
if ((Integer) value > 0) {
|
|
message("This device does not support hardware acceleration. " +
|
|
"In this case you can not change width or height. " +
|
|
"The view will act as WRAP_CONTENT by default.", true);
|
|
return false;
|
|
}
|
|
}
|
|
control.applyValue(camera, value);
|
|
BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel);
|
|
b.setState(BottomSheetBehavior.STATE_HIDDEN);
|
|
message("Changed " + control.getName() + " to " + name, false);
|
|
return true;
|
|
}
|
|
|
|
//region Permissions
|
|
|
|
@Override
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
boolean valid = true;
|
|
for (int grantResult : grantResults) {
|
|
valid = valid && grantResult == PackageManager.PERMISSION_GRANTED;
|
|
}
|
|
if (valid && !camera.isOpened()) {
|
|
camera.open();
|
|
}
|
|
}
|
|
|
|
//endregion
|
|
}
|
|
|