【问题标题】:Region of interest感兴趣的区域
【发布时间】:2011-08-10 02:11:19
【问题描述】:

我有兴趣只捕捉cameraPreview 的一部分。 我有一个矩形,它有一个 onTouchListener。我可以使用它在相机预览中的任何位置拖动我感兴趣的区域。 我的最终目标是只捕获预览的那部分。但我无法找到方法。 有没有我可以使用的 API? 任何人都可以在这里指导我。

谢谢

【问题讨论】:

    标签: android camera crop


    【解决方案1】:

    据我所知,API 中不支持该功能,但我已经成功完成了类似的操作。

    基本上你需要:

    • 实现 Camera.PreviewCallback
    • 将 YUV 预览帧解码为 RGB
    • 从 RGB 字节制作临时位图
    • 将屏幕上的矩形缩放到适合预览帧尺寸的大小
    • 最后,使用 createBitmap (Bitmap source, int x, int y, int width, int height) 从临时位图中裁剪出一个新位图。

    请注意,在 FROYO 通过 YuvImage 类之前,没有 API 支持 YUV 解码器,您可以使用类似的东西:

    YuvImage yuvimage=new YuvImage(yuv,ImageFormat.NV21,width,height,null);
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                yuvimage.compressToJpeg(new Rect(0,0,width,height), 100, baos);
                rgb =  BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
    

    如果您只支持 2.2 及更高版本,您可以跳过临时位图并使用 YuvImage.compressToJpeg 裁剪到您的缩放矩形。

    对于 2.2 之前的操作系统版本,您需要自己实现 YUV 解码,具体操作如下:

    /**
         * Decodes YUV frame to a buffer which can be use to create a bitmap
         * decode Y, U, and V values on the YUV 420 buffer described as YCbCr_422_SP by Android 
         * @param out the outgoing array of RGB bytes
         * @param fg the incoming frame bytes
         * @param width of source frame
         * @param height of source frame
         * @throws NullPointerException
         * @throws IllegalArgumentException
         */
        public static 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);
                }
            }
    
        }
    

    信用:http://groups.google.com/group/android-developers/browse_thread/thread/c85e829ab209ceea/3f180a16a4872b58?lnk=gst&q=onpreviewframe&pli=1

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多