【问题标题】:Extracting RGB values from Android camera从 Android 相机中提取 RGB 值
【发布时间】:2016-01-17 23:37:54
【问题描述】:

我正在尝试从 Android 相机预览中拍摄的图片中获取 RGB 值流。

所以我查了很多关于 Stackoverflow 和在线教程的问题,我已经走到了这一步:

设置以下相机属性:

        Camera.Parameters param = camera.getParameters();

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int swidth = size.x;
        int sheight = size.y;

        param.setPreviewSize(sheight, swidth);
        camera.setParameters(param);
        param.setPreviewFormat(ImageFormat.NV21);

        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
        camera.setDisplayOrientation(90);

param.setPreviewFormat(ImageFormat.NV21); 是为了兼容所有设备。

那么我有:

    jpegCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {

                int[] rgbs = new int[swidth*sheight]; //from above code
                decodeYUV(rgbs, data, swidth, sheight);
                for(int i = 0; i<rgbs.length; i++)
                    System.out.println("RGB: " + rgbs[i]);

其中decodeYUV() 是给定here on SO 的方法。我已经尝试使用这两个答案(方法),并且得到了类似的结果。这意味着它必须工作,我只是做错了。

现在,我假设它是 ARGB 格式。

我从上面的代码中得到以下输出流:

RGB: -16757489
RGB: -16059990
RGB: -9157
RGB: -49494
RGB: -2859008
RGB: -7283401
RGB: -4288512
RGB: -3339658
RGB: -6411776
RGB: -13994240
RGB: -16750475
RGB: -16735438
RGB: -14937280
RGB: -3866455
RGB: -16762040
RGB: -16714621
RGB: -11647630
RGB: -37121
...
...

如何从中提取 RGB 值,格式为 R/G/B = [0..255]

感谢您的帮助!

【问题讨论】:

    标签: android camera stream rgb


    【解决方案1】:

    如果格式是 ARGB 则:

    int argb = rgbs[i];
    int a = ( argb >> 24 ) & 255;
    int r = ( argb >> 16 ) & 255;
    int g = ( argb >> 8 ) & 255;
    int b = argb & 255;
    

    &gt;&gt; 运算符将 int 向右移动,&amp;&amp; 是一个布尔值,它掩盖了结果的最后八位。

    【讨论】:

    • 这肯定有帮助(假设您的意思是&amp; 而不是&amp;&amp;)。不幸的是,我得到了 RGB 值,但不是正确的值。即使我拍摄纯黑色照片,它似乎总是平均在 120/120/120 左右。我的解码函数中也出现了 Array Out of Bounds 。任何想法为什么会发生这种情况?我一直在遵循精确的步骤。
    • 很难说为什么你会在解码中遇到异常......你没有发布它。感谢您指出错字 - 已修复。
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 2011-02-19
    • 1970-01-01
    • 2018-12-01
    • 2010-11-06
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多