Colin Lee
@colinmlee
Colin Lee
Android Software Engineer
@ Vidku
Twitter: @colinmlee
Vidku: @colin
Video sharing for groups and friends.
Series A Startup Based in North Loop Minneapolis.
Download our new app on Android and iOS.
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);//create new Intent
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
// set MIME type
intent.setType("image/*");
// if multiple files are accepted
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, CAPTURE_VIDEO_GALLERY_REQUEST_CODE);
// handle returned data in OnActivityResult()Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
// set MIME type
intent.setType("video/*");
// if multiple files are accepted
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, CAPTURE_VIDEO_GALLERY_REQUEST_CODE);
// handle returned data in OnActivityResult()// initialize camera
Camera camera = Camera.open(cameraId); // usually 0 = back facing 1 = front facing
// set target surface for camera preview
try {
camera.setPreviewDisplay(mSurfaceView.getHolder());
} catch (IOException e) {
Log.e("Error setting up camera preview", e);
}
// start preview
camera.startPreview();
// when capture button is pressed send JPEG byte[] to a callback
camera.takePicture(null, null, jpegCallback);
// when it's time to clean up
camera.stopPreview();
camera.release();
camera = null;// https://github.com/googlesamples/android-Camera2Basic
// always request permissions first!
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}// always close and free the camera onPause!
private void closeCamera() {
try {
mCameraOpenCloseLock.acquire();
if (null != mCaptureSession) {
mCaptureSession.close();
mCaptureSession = null;
}
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
}
if (null != mImageReader) {
mImageReader.close();
mImageReader = null;
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
} finally {
mCameraOpenCloseLock.release();
}
}// lock focus before capture, unlock after capture
try {
// This is the CaptureRequest.Builder that we use to take a picture.
final CaptureRequest.Builder captureBuilder =
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mImageReader.getSurface());
// Orientation
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
mCaptureSession.stopRepeating();
mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}Invest in some old and cheap hardware. The emulator will not break in the same ways.
"I'm the LG Lucky.
$10-20 at Walmart."
mRecorder = new MediaRecorder();
camera.stopPreview();
camera.unlock();
mRecorder.setCamera(camera);
mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mRecorder.setProfile(mLegacyPreview.getProfile());
mFile = CameraUtil.getOutputMediaFile(CameraUtil.MEDIA_TYPE_VIDEO);
if (mFile != null) {
mRecorder.setOutputFile(mFile.getAbsolutePath());
// Set recording time limit
mRecorder.setMaxDuration(mMaximumRecordingDurationInMs);
// Set rotation of video recording
mRecorder.setOrientationHint(getRotation());
mRecorder.prepare();
}
// Camera is available and unlocked. you can start recording.
mRecorder.start();Use COLOR_FormatSurface to avoid color format incompatibility