pricing google example batch android android-camera android-mediarecorder google-vision android-vision

android - example - Grabadora de medios con la API de Google Vision



google vision api php (1)

Estoy usando el ejemplo FaceTracker de la API de Android Vision. Sin embargo, estoy experimentando dificultades para grabar videos mientras se dibujan las superposiciones en ellos.

Una forma es almacenar mapas de bits como imágenes y procesarlos utilizando FFmpeg o Xuggler para combinarlos como videos, pero me pregunto si existe una mejor solución a este problema si podemos grabar videos en tiempo de ejecución a medida que se proyecta la vista previa.

Actualización 1: actualicé la following clase con el grabador de medios, pero la grabación aún no funciona. Está lanzando el siguiente error cuando llamo a la función triggerRecording ():

MediaRecorder: inicio llamado en un estado no válido: 4

y tengo permiso de almacenamiento externo en el archivo Manifest.

Actualización 2:

He solucionado el problema anterior en el código y moví el setupMediaRecorder () en la devolución de llamada onSurfaceCreated. Sin embargo, cuando detengo la grabación, se inicia la excepción de tiempo de ejecución. De acuerdo con la documentation si no hay ningún video / audio, se lanzará una excepción en tiempo de ejecución.

Entonces, ¿qué me estoy perdiendo aquí?

public class CameraSourcePreview extends ViewGroup { private static final String TAG = "CameraSourcePreview"; 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); } private MediaRecorder mMediaRecorder; /** * Whether the app is recording video now */ private boolean mIsRecordingVideo; private Context mContext; private SurfaceView mSurfaceView; private boolean mStartRequested; private boolean mSurfaceAvailable; private CameraSource mCameraSource; private GraphicOverlay mOverlay; public CameraSourcePreview(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mStartRequested = false; mSurfaceAvailable = false; mSurfaceView = new SurfaceView(context); mSurfaceView.getHolder().addCallback(new SurfaceCallback()); addView(mSurfaceView); mMediaRecorder = new MediaRecorder(); } private void setUpMediaRecorder() throws IOException { mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface()); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DCIM + File.separator + System.currentTimeMillis() + ".mp4"); mMediaRecorder.setVideoEncodingBitRate(10000000); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(480, 640); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //int rotation = mContext.getWindowManager().getDefaultDisplay().getRotation(); //int orientation = ORIENTATIONS.get(rotation); mMediaRecorder.setOrientationHint(ORIENTATIONS.get(0)); mMediaRecorder.prepare(); mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() { @Override public void onError(MediaRecorder mr, int what, int extra) { Timber.d(mr.toString() + " : what[" + what + "]" + " Extras[" + extra + "]"); } }); } public void start(CameraSource cameraSource) throws IOException { if (cameraSource == null) { stop(); } mCameraSource = cameraSource; if (mCameraSource != null) { mStartRequested = true; startIfReady(); } } public void start(CameraSource cameraSource, GraphicOverlay overlay) throws IOException { mOverlay = overlay; start(cameraSource); } public void stop() { if (mCameraSource != null) { mCameraSource.stop(); } } public void release() { if (mCameraSource != null) { mCameraSource.release(); mCameraSource = null; } } private void startIfReady() throws IOException { if (mStartRequested && mSurfaceAvailable) { mCameraSource.start(mSurfaceView.getHolder()); if (mOverlay != null) { Size size = mCameraSource.getPreviewSize(); int min = Math.min(size.getWidth(), size.getHeight()); int max = Math.max(size.getWidth(), size.getHeight()); if (isPortraitMode()) { // Swap width and height sizes when in portrait, since it will be rotated by // 90 degrees mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing()); } else { mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing()); } mOverlay.clear(); } mStartRequested = false; } } private class SurfaceCallback implements SurfaceHolder.Callback { @Override public void surfaceCreated(SurfaceHolder surface) { mSurfaceAvailable = true; surface.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // setup the media recorder try { setUpMediaRecorder(); } catch (IOException e) { e.printStackTrace(); } try { startIfReady(); } catch (IOException e) { Timber.e(TAG, "Could not start camera source.", e); } } @Override public void surfaceDestroyed(SurfaceHolder surface) { mSurfaceAvailable = false; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int width = 320; int height = 240; if (mCameraSource != null) { Size size = mCameraSource.getPreviewSize(); if (size != null) { width = size.getWidth(); height = size.getHeight(); } } // Swap width and height sizes when in portrait, since it will be rotated 90 degrees if (isPortraitMode()) { int tmp = width; width = height; height = tmp; } final int layoutWidth = right - left; final int layoutHeight = bottom - top; // Computes height and width for potentially doing fit width. int childWidth = layoutWidth; int childHeight = (int) (((float) layoutWidth / (float) width) * height); // If height is too tall using fit width, does fit height instead. if (childHeight > layoutHeight) { childHeight = layoutHeight; childWidth = (int) (((float) layoutHeight / (float) height) * width); } for (int i = 0; i < getChildCount(); ++i) { getChildAt(i).layout(0, 0, childWidth, childHeight); } try { startIfReady(); } catch (IOException e) { Timber.e(TAG, "Could not start camera source.", e); } } private boolean isPortraitMode() { int orientation = mContext.getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) { return false; } if (orientation == Configuration.ORIENTATION_PORTRAIT) { return true; } Timber.d(TAG, "isPortraitMode returning false by default"); return false; } private void startRecordingVideo() { try { // Start recording mMediaRecorder.start(); mIsRecordingVideo = true; } catch (IllegalStateException e) { e.printStackTrace(); } } private void stopRecordingVideo() { // UI mIsRecordingVideo = false; // Stop recording mMediaRecorder.stop(); mMediaRecorder.reset(); } public void triggerRecording() { if (mIsRecordingVideo) { stopRecordingVideo(); Timber.d("Recording stopped"); } else { startRecordingVideo(); Timber.d("Recording starting"); } } }


Solución 1: desde Android Lollipop, se introdujo una API de MediaProjection que, junto con MediaRecorder, se puede usar para guardar un SurfaceView en un archivo de video. Este ejemplo muestra cómo enviar un SurfaceView a un archivo de video.

Solución 2: Alternativamente, puede usar una de las clases de codificador ordenadas que se proporcionan en el repositorio de Grafika . Tenga en cuenta que esto requerirá que FaceTracker aplicación FaceTracker para que esté utilizando OpenGL para realizar todo el renderizado. Esto se debe a que las muestras de Grafika utilizan el canal OpenGL para una lectura y escritura rápida de los datos de textura.

Hay un ejemplo mínimo que logra exactamente lo que quiere usando un CircularEncoder en la clase ContinuousCaptureActivity . Esto proporciona un ejemplo de Frame Blitting , que muestra simultáneamente los datos del buffer de cuadro en la pantalla y la salida a un video.

El cambio principal sería utilizar Grafika WindowSurface lugar de un SurfaceView para la aplicación FaceTracker, esto configura el Contexto EGL que le permite guardar datos de búfer de cuadros en un archivo a través del Codificador. Una vez que puede renderizar todo a WindowSurface, es trivial configurar la grabación de la misma manera que la clase ContinuousCaptureActivity .