studio programacion herramientas fundamentos con avanzado aplicaciones java android camera

java - programacion - manual de android en pdf



Obtener marcos de la imagen de video en Android (4)

Implementé una aplicación simple que muestra la imagen de la cámara en la pantalla. Lo que me gusta hacer ahora es tomar un solo fotograma y procesarlo como mapa de bits. Por lo que pude averiguar hasta este punto, no es algo fácil de hacer.

Intenté usar el método onPreviewFrame con el que obtienes el fotograma actual como una matriz de bytes e intenté decodificarlo con la clase BitmapFactory pero devuelve null. El formato del marco es un YUV sin encabezado que podría traducirse a mapa de bits, pero lleva demasiado tiempo en un teléfono. También he leído que el método onPreviewFrame tiene inconvenientes en el tiempo de ejecución, si la aplicación tarda demasiado en bloquearse.

Entonces, ¿cuál es la forma correcta de hacer esto?


De hecho, probé el código dado que la respuesta anterior descubrió que los valores de color no son exactos. Lo comprobé tomando tanto la vista previa como la cámara. Tomar una fotografía que devuelve directamente una matriz JPEG. Y los colores fueron muy diferentes. Después de buscar un poco más encontré otro ejemplo para convertir la imagen previa de YCrCb a RGB:

static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) { final int frameSize = width * height; for (int j = 0, yp = 0; j < height; j++) { int uvp = frameSize + (j >> 1) * width, u = 0, v = 0; for (int i = 0; i < width; i++, yp++) { int y = (0xff & ((int) yuv420sp[yp])) - 16; if (y < 0) y = 0; if ((i & 1) == 0) { v = (0xff & yuv420sp[uvp++]) - 128; u = (0xff & yuv420sp[uvp++]) - 128; } int y1192 = 1192 * y; int r = (y1192 + 1634 * v); int g = (y1192 - 833 * v - 400 * u); int b = (y1192 + 2066 * u); if (r < 0) r = 0; else if (r > 262143) r = 262143; if (g < 0) g = 0; else if (g > 262143) g = 262143; if (b < 0) b = 0; else if (b > 262143) b = 262143; rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff); } } }

Los valores de color dados por this y the takePicture () coinciden exactamente. Pensé que debería publicarlo aquí. De aquí obtuve este código. Espero que esto ayude.


En API 17+, puede hacer la conversión a RGBA888 desde NV21 con el RenderScript ''ScriptIntrinsicYuvToRGB''. Esto le permite procesar fácilmente tramas de vista previa sin codificar / decodificar manualmente cuadros:

@Override public void onPreviewFrame(byte[] data, Camera camera) { Bitmap bitmap = Bitmap.createBitmap(r.width(), r.height(), Bitmap.Config.ARGB_8888); Allocation bmData = renderScriptNV21ToRGBA888( mContext, r.width(), r.height(), data); bmData.copyTo(bitmap); } public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) { RenderScript rs = RenderScript.create(context); ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length); Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height); Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); in.copyFrom(nv21); yuvToRgbIntrinsic.setInput(in); yuvToRgbIntrinsic.forEach(out); return out; }


La solución RenderScript de Tim es genial. Dos comentarios aquí sin embargo:

  1. Cree y reutilice RenderScript rs , y Allocation in, out . Crearlos en cada cuadro dañará el rendimiento.
  2. La biblioteca de soporte de RenderScript puede ayudarlo a respaldar la compatibilidad con Android 2.3.

Ok, lo que terminamos haciendo es usar el método onPreviewFrame y decodificar los datos en un subproceso separado utilizando un método que se puede encontrar en el grupo de ayuda de Android.

decodeYUV(argb8888, data, camSize.width, camSize.height); Bitmap bitmap = Bitmap.createBitmap(argb8888, camSize.width, camSize.height, Config.ARGB_8888);

...

// decode Y, U, and V values on the YUV 420 buffer described as YCbCr_422_SP by Android // David Manpearl 081201 public void decodeYUV(int[] out, byte[] fg, int width, int height) throws NullPointerException, IllegalArgumentException { int sz = width * height; if (out == null) throw new NullPointerException("buffer out is null"); if (out.length < sz) throw new IllegalArgumentException("buffer out size " + out.length + " < minimum " + sz); if (fg == null) throw new NullPointerException("buffer ''fg'' is null"); if (fg.length < sz) throw new IllegalArgumentException("buffer fg size " + fg.length + " < minimum " + sz * 3 / 2); int i, j; int Y, Cr = 0, Cb = 0; for (j = 0; j < height; j++) { int pixPtr = j * width; final int jDiv2 = j >> 1; for (i = 0; i < width; i++) { Y = fg[pixPtr]; if (Y < 0) Y += 255; if ((i & 0x1) != 1) { final int cOff = sz + jDiv2 * width + (i >> 1) * 2; Cb = fg[cOff]; if (Cb < 0) Cb += 127; else Cb -= 128; Cr = fg[cOff + 1]; if (Cr < 0) Cr += 127; else Cr -= 128; } int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5); if (R < 0) R = 0; else if (R > 255) R = 255; int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5); if (G < 0) G = 0; else if (G > 255) G = 255; int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6); if (B < 0) B = 0; else if (B > 255) B = 255; out[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R; } } }

Enlace: http://groups.google.com/group/android-developers/browse_thread/thread/c85e829ab209ceea/3f180a16a4872b58?lnk=gst&q=onpreviewframe#3f180a16a4872b58