【问题标题】:Android camera pixel manipulationAndroid相机像素操作
【发布时间】:2012-11-25 03:38:17
【问题描述】:

我正在尝试构建一个相机应用程序,该应用程序接受相机预览,操作像素,然后显示新图像。我需要实时进行操作。

根据我在网上阅读的内容以及此处的问题,您需要制作自定义表面视图并通过onPreviewFrame 方法操作像素阵列。我已经构建了一个自定义表面视图并运行此方法。我已将YUV 转换为RGB

现在,我的问题是,如何在屏幕上实时显示这个新的像素阵列?我是否以某种方式在onPreviewFrame 方法中返回它?我必须更改byte[] 数组吗?我是否需要使用新的像素阵列并使用位图显示它?有没有办法在不显示预览的情况下从相机预览中获取byte[] 数组?

如果有人能用代码示例回答这些问题,那就太好了!我对 Android 有点陌生,所以我需要解释清楚的答案让我理解。这是我运行相机预览的部分代码:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // start preview with new settings
        try {
            //parameters.setPreviewSize(w, h);
            mCamera.setParameters(parameters);
            mCamera.setPreviewDisplay(mHolder);
            mCamera.setPreviewCallback(new PreviewCallback() {
                public void onPreviewFrame(byte[] data, Camera camera)
                {
                    System.out.println("onPreviewFrame");
                    //transforms NV21 pixel data into RGB pixels  
                    decodeYUV420SP(pixels, data, previewSize.width,  previewSize.height);  

//Outuput the value of the top left pixel in the preview to LogCat  
                    Log.i("Pixels", "The top right pixel has the following RGB (hexadecimal) values:"  
                           +Integer.toHexString(pixels[0]));  


                }
            });
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(null, "Error starting camera preview: " + e.getMessage());
        }
    }

这给了我想要显示而不是预览的 rgb 像素数组。我该怎么做?

【问题讨论】:

    标签: android image-processing camera surfaceview


    【解决方案1】:

    您无法操作连接到相机预览的表面。您在onPreviewFrame() 中收到的字节数组只是框架在屏幕上显示的内容的副本。此外,您会发现这两个流是异步的:您可以减慢回调的速度(例如,通过在回调中添加一些 sleep()),但预览界面仍然会更新。

    您可以通过在其上放置其他视图来隐藏预览SurfaceView,或者您可以通过使用setPreviewTexture() 而不是setPreviewDisplay() 完全摆脱此视图(注意: 在 API 级别 11 中添加)。隐藏表面并不像看起来那么容易:框架可能会将其弹出到顶部,它需要仔细同步相机启动或重新启动与布局。

    无论如何,隐藏表面后,您可以使用onPreviewFrame() 中接收到的字节数组生成图像并显示它。您可以根据自己的喜好随意操作像素。我认为最佳技术是将像素数据发送到 OpenGL:您可以使用着色器将 YCrCb (NV21) 到 RGB 的转换卸载到 GPU。

    【讨论】:

    • 能否给出一个代码示例,将 rgb 像素的 int 数组更改为图像,然后如何在surfaceView 顶部显示该图像?
    • ImageView 太慢了,不能用作相机预览,因此像素数组到位图的转换是无关紧要的。您可以在anddev.org/viewforum.php?f=2 学习更高效的技术
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多