|
| 1 | +package com.otaliastudios.cameraview.demo; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.content.pm.PackageManager; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.support.design.widget.BottomSheetBehavior; |
| 9 | +import android.support.v7.app.AppCompatActivity; |
| 10 | +import android.util.Log; |
| 11 | +import android.view.View; |
| 12 | +import android.view.ViewGroup; |
| 13 | +import android.view.ViewTreeObserver; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import com.otaliastudios.cameraview.Audio; |
| 17 | +import com.otaliastudios.cameraview.CameraListener; |
| 18 | +import com.otaliastudios.cameraview.CameraLogger; |
| 19 | +import com.otaliastudios.cameraview.CameraOptions; |
| 20 | +import com.otaliastudios.cameraview.CameraView; |
| 21 | +import com.otaliastudios.cameraview.Flash; |
| 22 | +import com.otaliastudios.cameraview.Grid; |
| 23 | +import com.otaliastudios.cameraview.SessionType; |
| 24 | +import com.otaliastudios.cameraview.Size; |
| 25 | +import com.otaliastudios.cameraview.VideoQuality; |
| 26 | +import com.otaliastudios.cameraview.WhiteBalance; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | + |
| 30 | + |
| 31 | +public class CameraActivity extends AppCompatActivity implements View.OnClickListener, ControlView.Callback { |
| 32 | + |
| 33 | + private CameraView camera; |
| 34 | + private ViewGroup controlPanel; |
| 35 | + |
| 36 | + private boolean mCapturingPicture; |
| 37 | + private boolean mCapturingVideo; |
| 38 | + |
| 39 | + // To show stuff in the callback |
| 40 | + private Size mCaptureNativeSize; |
| 41 | + private long mCaptureTime; |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void onCreate(Bundle savedInstanceState) { |
| 45 | + super.onCreate(savedInstanceState); |
| 46 | + setContentView(R.layout.activity_camera); |
| 47 | + CameraLogger.setLogLevel(CameraLogger.LEVEL_VERBOSE); |
| 48 | + |
| 49 | + camera = findViewById(R.id.camera); |
| 50 | + camera.addCameraListener(new CameraListener() { |
| 51 | + public void onCameraOpened(CameraOptions options) { onOpened(); } |
| 52 | + public void onPictureTaken(byte[] jpeg) { onPicture(jpeg); } |
| 53 | + public void onVideoTaken(File video) { onVideo(video); } |
| 54 | + }); |
| 55 | + |
| 56 | + findViewById(R.id.edit).setOnClickListener(this); |
| 57 | + findViewById(R.id.capturePhoto).setOnClickListener(this); |
| 58 | + findViewById(R.id.captureVideo).setOnClickListener(this); |
| 59 | + findViewById(R.id.toggleCamera).setOnClickListener(this); |
| 60 | + |
| 61 | + controlPanel = findViewById(R.id.controls); |
| 62 | + ViewGroup group = (ViewGroup) controlPanel.getChildAt(0); |
| 63 | + Control[] controls = Control.values(); |
| 64 | + for (Control control : controls) { |
| 65 | + ControlView view = new ControlView(this, control, this); |
| 66 | + group.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, |
| 67 | + ViewGroup.LayoutParams.WRAP_CONTENT); |
| 68 | + } |
| 69 | + |
| 70 | + controlPanel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
| 71 | + @Override |
| 72 | + public void onGlobalLayout() { |
| 73 | + BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel); |
| 74 | + b.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private void message(String content, boolean important) { |
| 80 | + int length = important ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT; |
| 81 | + Toast.makeText(this, content, length).show(); |
| 82 | + } |
| 83 | + |
| 84 | + private void onOpened() { |
| 85 | + ViewGroup group = (ViewGroup) controlPanel.getChildAt(0); |
| 86 | + for (int i = 0; i < group.getChildCount(); i++) { |
| 87 | + ControlView view = (ControlView) group.getChildAt(i); |
| 88 | + view.onCameraOpened(camera); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void onPicture(byte[] jpeg) { |
| 93 | + mCapturingPicture = false; |
| 94 | + long callbackTime = System.currentTimeMillis(); |
| 95 | + if (mCapturingVideo) { |
| 96 | + message("Captured while taking video. Size="+mCaptureNativeSize, false); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + // This can happen if picture was taken with a gesture. |
| 101 | + if (mCaptureTime == 0) mCaptureTime = callbackTime - 300; |
| 102 | + if (mCaptureNativeSize == null) mCaptureNativeSize = camera.getCaptureSize(); |
| 103 | + |
| 104 | + PicturePreviewActivity.setImage(jpeg); |
| 105 | + Intent intent = new Intent(CameraActivity.this, PicturePreviewActivity.class); |
| 106 | + intent.putExtra("delay", callbackTime - mCaptureTime); |
| 107 | + intent.putExtra("nativeWidth", mCaptureNativeSize.getWidth()); |
| 108 | + intent.putExtra("nativeHeight", mCaptureNativeSize.getHeight()); |
| 109 | + startActivity(intent); |
| 110 | + |
| 111 | + mCaptureTime = 0; |
| 112 | + mCaptureNativeSize = null; |
| 113 | + } |
| 114 | + |
| 115 | + private void onVideo(File video) { |
| 116 | + mCapturingVideo = false; |
| 117 | + Intent intent = new Intent(CameraActivity.this, VideoPreviewActivity.class); |
| 118 | + intent.putExtra("video", Uri.fromFile(video)); |
| 119 | + startActivity(intent); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onClick(View view) { |
| 124 | + switch (view.getId()) { |
| 125 | + case R.id.edit: edit(); break; |
| 126 | + case R.id.capturePhoto: capturePhoto(); break; |
| 127 | + case R.id.captureVideo: captureVideo(); break; |
| 128 | + case R.id.toggleCamera: toggleCamera(); break; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void edit() { |
| 133 | + BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel); |
| 134 | + b.setState(BottomSheetBehavior.STATE_COLLAPSED); |
| 135 | + } |
| 136 | + |
| 137 | + private void capturePhoto() { |
| 138 | + if (mCapturingPicture) return; |
| 139 | + mCapturingPicture = true; |
| 140 | + mCaptureTime = System.currentTimeMillis(); |
| 141 | + mCaptureNativeSize = camera.getCaptureSize(); |
| 142 | + message("Capturing picture...", false); |
| 143 | + camera.capturePicture(); |
| 144 | + } |
| 145 | + |
| 146 | + private void captureVideo() { |
| 147 | + if (camera.getSessionType() != SessionType.VIDEO) { |
| 148 | + message("Can't record video while session type is 'picture'.", false); |
| 149 | + return; |
| 150 | + } |
| 151 | + if (mCapturingPicture || mCapturingVideo) return; |
| 152 | + mCapturingVideo = true; |
| 153 | + message("Recording for 8 seconds...", true); |
| 154 | + camera.startCapturingVideo(null, 8000); |
| 155 | + } |
| 156 | + |
| 157 | + private void toggleCamera() { |
| 158 | + if (mCapturingPicture) return; |
| 159 | + switch (camera.toggleFacing()) { |
| 160 | + case BACK: |
| 161 | + message("Switched to back camera!", false); |
| 162 | + break; |
| 163 | + |
| 164 | + case FRONT: |
| 165 | + message("Switched to front camera!", false); |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onValueChanged(Control control, Object value, String name) { |
| 172 | + control.applyValue(camera, value); |
| 173 | + BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel); |
| 174 | + b.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 175 | + message("Changed " + control.getName() + " to " + name, false); |
| 176 | + } |
| 177 | + |
| 178 | + //region Boilerplate |
| 179 | + |
| 180 | + @Override |
| 181 | + protected void onResume() { |
| 182 | + super.onResume(); |
| 183 | + camera.start(); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + protected void onPause() { |
| 188 | + super.onPause(); |
| 189 | + camera.stop(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + protected void onDestroy() { |
| 194 | + super.onDestroy(); |
| 195 | + camera.destroy(); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 200 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 201 | + boolean valid = true; |
| 202 | + for (int grantResult : grantResults) { |
| 203 | + valid = valid && grantResult == PackageManager.PERMISSION_GRANTED; |
| 204 | + } |
| 205 | + if (valid && !camera.isStarted()) { |
| 206 | + camera.start(); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + //endregion |
| 211 | +} |
0 commit comments