获取图像从SurfaceView到ImageView的?图像、SurfaceView、ImageView

2023-09-05 05:18:07 作者:Autism(孤独症)

我有获取图像/可绘制或从该工程作为摄像头preivew一个SurfaceView的位图的有点麻烦了。

 最后CameraSurfaceView cameraSurfaceView =新CameraSurfaceView(本);
    的LinearLayout LL =(的LinearLayout)findViewById(R.id.linearLayout1);
    ll.addView(cameraSurfaceView); //这个工程

    ImageView的ivCam =(ImageView的)findViewById(R.id.ivCam);
    ivCam.setImageBitmap(cameraSurfaceView.getDrawingCache()); //这并不:(
 

有什么建议?谢谢!

编辑:

  @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        最后CameraSurfaceView cameraSurfaceView =新CameraSurfaceView(本);
        的LinearLayout LL =(的LinearLayout)findViewById(R.id.LLS);
        ll.addView(cameraSurfaceView); //这个工程


    }

////////////////////////////////////////////////// /////////////////////////////////////////////

    公共类CameraSurfaceView扩展了SurfaceView实现SurfaceHolder.Callback
    {
            私人SurfaceHolder持有人;
            私人摄像头摄像头;

            公共CameraSurfaceView(上下文的背景下)
            {
                    超(上下文);

                    //启动地面支架正确
                    this.holder = this.getHolder();
                    this.holder.addCallback(本);
                    this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            }

            @覆盖
            公共无效surfaceCreated(SurfaceHolder持有人)
            {
                    尝试
                    {
                            this.camera = Camera.open();
                            this.camera.set previewDisplay(this.holder);

                            this.camera.set previewCallback(新previewCallback(){

                              在previewFrame(byte []的_data,摄像机_camera){公共无效

                                Camera.Parameters PARAMS = _camera.getParameters();
                                   。INT W = params.get previewSize()宽度;
                                   。INT H = params.get previewSize()高度;
                                   INT格式= params.get previewFormat();
                                   YuvImage形象=新YuvImage(_data,格式,W,H,NULL);

                                   ByteArrayOutputStream OUT =新ByteArrayOutputStream();
                                   矩形面积=新的Rect(0,0,W,H);
                                   image.com pressToJpeg(区,50出);
                                   位图BM = BitmapFactory.de codeByteArray(out.toByteArray(),0,out.size());

                                   ImageView的ivCam =(ImageView的)findViewById(R.id.imageView1);
                                   ivCam.setImageBitmap(BM); ///零点EX这里!
                              }
                            });

                    }
                    赶上(IOException异常IOE)
                    {
                            ioe.printStackTrace(的System.out);
                    }
            }


            @覆盖
            公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT)
            {
                this.camera.start preVIEW();
            }

            @覆盖
            公共无效surfaceDestroyed(SurfaceHolder持有人)
            {
                this.camera.stop preVIEW();
                this.camera.release();
                this.camera = NULL;
            }


            公共相机getCamera的()
            {
                    返回this.camera;
            }


    }
 

解决方案

这是远远比这更复杂。在SurfaceView的背景是不是相机preVIEW。你必须有一个类实现相机。previewCalback 。一旦你有,你可以得到一个包含了preVIEW发送图像的字节数组。在一些手机上,您可以设置preVIEW是一个JPEG在这种情况下,你可以去code它直接与BitmapFactory。在不支持该功能的其他手机,你会在默认情况下得到YUV 4:2:0的图像,你必须要转换为JPEG图像

在Android 2.2+,您可以将YUV图像转换为JPEG,像这样:

  INT W = params.get previewSize()宽度;
   。INT H = params.get previewSize()高度;
   INT格式= params.get previewFormat();
   YuvImage图象=新YuvImage(数据,格式,W,H,空);

   ByteArrayOutputStream OUT =新ByteArrayOutputStream();
   矩形面积=新的Rect(0,0,W,H);
   image.com pressToJpeg(区,50出);
   位图BM = BitmapFactory.de codeByteArray(out.toByteArray(),0,out.size());
   ivCam.setImageBitmap(BM);
 
11日八大热门本 苹果ipad Air仅售3500元

如果您定位老款车型,你必须使用一个转换算法喜欢这里的人。

的http://blog.tomgibara.com/post/132956174/yuv420-to-rgb565-conversion-in-android

一个SO来源:

获取来自视频图像帧的Andr​​oid

编辑: 如果你想要的是显示摄像机视图,那么你只需要添加的 SurfaceView 您的相机正在使用像你在你的问题确实已经显示的布局。它已经显示出来。

I'm having a little trouble of getting an image/drawable or a bitmap from a SurfaceView that works as a camera preivew.

    final CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(this);
    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
    ll.addView(cameraSurfaceView); // THIS WORKS

    ImageView ivCam = (ImageView) findViewById(R.id.ivCam);
    ivCam.setImageBitmap(cameraSurfaceView.getDrawingCache()); // THIS DOESN'T :(

Any suggestions? Thanks!

EDIT:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(this);
        LinearLayout ll = (LinearLayout)findViewById(R.id.LLS);
        ll.addView(cameraSurfaceView); // THIS WORKS


    }

///////////////////////////////////////////////////////////////////////////////////////////////   

    public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
    {
            private SurfaceHolder holder;
            private Camera camera;

            public CameraSurfaceView(Context context) 
            {
                    super(context);

                    //Initiate the Surface Holder properly
                    this.holder = this.getHolder();
                    this.holder.addCallback(this);
                    this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) 
            {
                    try
                    {
                            this.camera = Camera.open();
                            this.camera.setPreviewDisplay(this.holder);

                            this.camera.setPreviewCallback(new PreviewCallback() {

                              public void onPreviewFrame(byte[] _data, Camera _camera) {

                                Camera.Parameters params = _camera.getParameters();
                                   int w = params.getPreviewSize().width;
                                   int h = params.getPreviewSize().height;
                                   int format = params.getPreviewFormat();
                                   YuvImage image = new YuvImage(_data, format, w, h, null);

                                   ByteArrayOutputStream out = new ByteArrayOutputStream();
                                   Rect area = new Rect(0, 0, w, h);
                                   image.compressToJpeg(area, 50, out);
                                   Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());

                                   ImageView ivCam = (ImageView) findViewById(R.id.imageView1);
                                   ivCam.setImageBitmap(bm);  /// NULL POINT EX HERE!
                              }
                            });

                    }
                    catch(IOException ioe)
                    {
                            ioe.printStackTrace(System.out);
                    }
            }


            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
            {
                this.camera.startPreview();
            }    

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) 
            {
                this.camera.stopPreview();
                this.camera.release();
                this.camera = null;
            }


            public Camera getCamera()
            {
                    return this.camera;
            }


    }

解决方案

It's far more complicated than that. The background of the SurfaceView is not the camera preview. You have to have a class that implements Camera.PreviewCalback. Once you have that, you can get a byte array containing the image that the preview sends. On some phones, you can set the preview to be a JPEG in which case you can decode it straight with BitmapFactory. On other phones that don't support that feature, you'll get by default a YUV 4:2:0 image that you have to convert into a JPEG image.

On Android 2.2+, you can convert the YUV image to a JPEG like so:

   int w = params.getPreviewSize().width;
   int h = params.getPreviewSize().height;
   int format = params.getPreviewFormat();
   YuvImage image = new YuvImage(data, format, w, h, null);

   ByteArrayOutputStream out = new ByteArrayOutputStream();
   Rect area = new Rect(0, 0, w, h);
   image.compressToJpeg(area, 50, out);
   Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
   ivCam.setImageBitmap(bm);

If you're targeting older models, you have to use a conversion algorithm like the one here.

http://blog.tomgibara.com/post/132956174/yuv420-to-rgb565-conversion-in-android

A SO source:

Getting frames from Video Image in Android

EDIT: If all you want is to show the camera view, then you just add the SurfaceView that your camera is using to a layout that is already displayed like you did in your question. It's already displaying it.