/* * Copyright 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.camera2video; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.Fragment; import android.content.Context; import android.content.DialogInterface; import android.content.res.Configuration; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.SurfaceTexture; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CameraMetadata; import android.hardware.camera2.CaptureRequest; import android.hardware.camera2.params.StreamConfigurationMap; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.util.Size; import android.util.SparseIntArray; import android.view.LayoutInflater; import android.view.Surface; import android.view.TextureView; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Camera2VideoFragment extends Fragment implements View.OnClickListener { private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** * An {@link AutoFitTextureView} for camera preview. */ private AutoFitTextureView mTextureView; /** * Button to record video */ private Button mButtonVideo; /** * A refernce to the opened {@link android.hardware.camera2.CameraDevice}. */ private CameraDevice mCameraDevice; /** * A reference to the current {@link android.hardware.camera2.CameraCaptureSession} for preview. */ private CameraCaptureSession mPreviewSession; /** * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a * {@link TextureView}. */ private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { configureTransform(width, height); startPreview(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) { configureTransform(width, height); startPreview(); } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } }; /** * The {@link android.util.Size} of camera preview. */ private Size mPreviewSize; /** * Camera preview. */ private CaptureRequest.Builder mPreviewBuilder; /** * MediaRecorder */ private MediaRecorder mMediaRecorder; /** * Whether the app is recording video now */ private boolean mIsRecordingVideo; /** * Whether the app is currently trying to open camera */ private boolean mOpeningCamera; /** * {@link CameraDevice.StateListener} is called when {@link CameraDevice} changes its status. */ private CameraDevice.StateListener mStateListener = new CameraDevice.StateListener() { @Override public void onOpened(CameraDevice cameraDevice) { mCameraDevice = cameraDevice; startPreview(); mOpeningCamera = false; if (null != mTextureView) { configureTransform(mTextureView.getWidth(), mTextureView.getHeight()); } } @Override public void onDisconnected(CameraDevice cameraDevice) { cameraDevice.close(); mCameraDevice = null; mOpeningCamera = false; } @Override public void onError(CameraDevice cameraDevice, int error) { cameraDevice.close(); mCameraDevice = null; Activity activity = getActivity(); if (null != activity) { activity.finish(); } mOpeningCamera = false; } }; public static Camera2VideoFragment newInstance() { Camera2VideoFragment fragment = new Camera2VideoFragment(); fragment.setRetainInstance(true); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_camera2_video, container, false); } @Override public void onViewCreated(final View view, Bundle savedInstanceState) { mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture); mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); mButtonVideo = (Button) view.findViewById(R.id.video); mButtonVideo.setOnClickListener(this); view.findViewById(R.id.info).setOnClickListener(this); } @Override public void onResume() { super.onResume(); openCamera(); } @Override public void onPause() { super.onPause(); if (null != mCameraDevice) { mCameraDevice.close(); mCameraDevice = null; } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.video: { if (mIsRecordingVideo) { stopRecordingVideo(); } else { startRecordingVideo(); } break; } case R.id.info: { Activity activity = getActivity(); if (null != activity) { new AlertDialog.Builder(activity) .setMessage(R.string.intro_message) .setPositiveButton(android.R.string.ok, null) .show(); } break; } } } /** * Tries to open a {@link CameraDevice}. The result is listened by `mStateListener`. */ private void openCamera() { final Activity activity = getActivity(); if (null == activity || activity.isFinishing() || mOpeningCamera) { return; } mOpeningCamera = true; CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); try { String cameraId = manager.getCameraIdList()[0]; CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0]; int orientation = getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) { mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight()); } else { mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth()); } manager.openCamera(cameraId, mStateListener, null); } catch (CameraAccessException e) { Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show(); activity.finish(); } catch (NullPointerException e) { // Currently an NPE is thrown when the Camera2API is used but not supported on the // device this code runs. new ErrorDialog().show(getFragmentManager(), "dialog"); } } /** * Start the camera preview. */ private void startPreview() { if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) { return; } try { SurfaceTexture texture = mTextureView.getSurfaceTexture(); assert texture != null; texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); Surface surface = new Surface(texture); List surfaces = new ArrayList(); surfaces.add(surface); mPreviewBuilder.addTarget(surface); mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateListener() { @Override public void onConfigured(CameraCaptureSession cameraCaptureSession) { mPreviewSession = cameraCaptureSession; updatePreview(); } @Override public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { Activity activity = getActivity(); if (null != activity) { Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show(); } } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } /** * Update the camera preview. {@link #startPreview()} needs to be called in advance. */ private void updatePreview() { if (null == mCameraDevice) { return; } try { setUpCaptureRequestBuilder(mPreviewBuilder); HandlerThread thread = new HandlerThread("CameraPreview"); thread.start(); Handler backgroundHandler = new Handler(thread.getLooper()); mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, backgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } private void setUpCaptureRequestBuilder(CaptureRequest.Builder builder) { builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); } /** * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. * This method should not to be called until the camera preview size is determined in * openCamera, or until the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` */ private void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity(); if (null == mTextureView || null == mPreviewSize || null == activity) { return; } int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max( (float) viewHeight / mPreviewSize.getHeight(), (float) viewWidth / mPreviewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } mTextureView.setTransform(matrix); } private void startRecordingVideo() { final Activity activity = getActivity(); if (null == activity) { return; } mMediaRecorder = new MediaRecorder(); final File file = getVideoFile(activity); try { // UI mButtonVideo.setText(R.string.stop); mIsRecordingVideo = true; // Configure the MediaRecorder mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setOutputFile(file.getAbsolutePath()); mMediaRecorder.setVideoEncodingBitRate(10000000); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(1440, 1080); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int orientation = ORIENTATIONS.get(rotation); mMediaRecorder.setOrientationHint(orientation); mMediaRecorder.prepare(); Surface surface = mMediaRecorder.getSurface(); // Set up the CaptureRequest final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); builder.addTarget(surface); Surface previewSurface = new Surface(mTextureView.getSurfaceTexture()); builder.addTarget(previewSurface); mCameraDevice.createCaptureSession(Arrays.asList(surface, previewSurface), new CameraCaptureSession.StateListener() { @Override public void onConfigured(CameraCaptureSession session) { // Start recording try { session.setRepeatingRequest(builder.build(), null, null); mMediaRecorder.start(); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(CameraCaptureSession session) { Toast.makeText(activity, "Failed.", Toast.LENGTH_SHORT).show(); } }, null ); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (CameraAccessException e) { e.printStackTrace(); } } private File getVideoFile(Context context) { return new File(context.getExternalFilesDir(null), "video.mp4"); } private void stopRecordingVideo() { // UI mIsRecordingVideo = false; mButtonVideo.setText(R.string.record); // Stop recording mMediaRecorder.stop(); mMediaRecorder.release(); mMediaRecorder = null; Activity activity = getActivity(); if (null != activity) { Toast.makeText(activity, "Video saved: " + getVideoFile(activity), Toast.LENGTH_SHORT).show(); } startPreview(); } public static class ErrorDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity activity = getActivity(); return new AlertDialog.Builder(activity) .setMessage("This device doesn't support Camera2 API.") .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { activity.finish(); } }) .create(); } } }