图像叠加到摄像头preVIEW SurfaceView以及如何保存呢? [英] Overlay images onto Camera preview SurfaceView and how to save them?

查看:128
本文介绍了图像叠加到摄像头preVIEW SurfaceView以及如何保存呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被用于绘制图像的SurfaceView,我想它们叠加到一个活饲料从手机的摄像头。我已阅读<一个href="http://stackoverflow.com/questions/3548666/overlay-images-onto-camera-$p$pview-surfaceview">Overlay图像到相机preVIEW surfaceview 和<一href="http://stackoverflow.com/questions/2933882/how-to-draw-an-overlay-on-a-surfaceview-used-by-camera-on-android">how绘制覆盖在Android上使用相机surfaceview

 公共类TestCameraOverlayActivity延伸活动{
         点阵位图;

         / **第一次创建活动时调用。 * /
         @覆盖
         公共无效的onCreate(包savedInstanceState){
               super.onCreate(savedInstanceState);
               requestWindowFeature(Window.FEATURE_NO_TITLE);
               preVIEW米preVIEW =新的preVIEW(本);
               DrawOnTop mDraw =新DrawOnTop(本);
               位= BitmapFactory.de codeResource(getResources(),R.drawable.icon);
               的setContentView(M preVIEW);
               addContentView(mDraw,新的LayoutParams(LayoutParams.WRAP_CONTENT,
               LayoutParams.WRAP_CONTENT));
        }

        类DrawOnTop扩展视图{
            公共DrawOnTop(上下文的背景下){
                超(上下文);

        }

        @覆盖
        保护无效的OnDraw(帆布油画){
            涂料粉刷=新的油漆();
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
            canvas.drawText(测试文本,10,10,油漆);
            canvas.drawBitmap(位图,0,0,NULL);
            super.onDraw(画布);
        }
    }

    类preVIEW延伸SurfaceView实现SurfaceHolder.Callback {
        SurfaceHolder mHolder;
        相机mCamera;

        preVIEW(上下文的背景下){
            超(上下文);
            //安装SurfaceHolder.Callback所以我们得到通知时,该
            //下垫面创建和销毁。
            mHolder = getHolder();
            mHolder.addCallback(本);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        公共无效surfaceCreated(SurfaceHolder持有者){
            //表面经创建,获取摄像机,并告诉它在哪里
            //绘制。
            mCamera = Camera.open();
             尝试 {
        mCamera.set previewDisplay(保持器);
        }赶上(IOException异常E){

            e.printStackTrace();
        }
        }

        公共无效surfaceDestroyed(SurfaceHolder持有者){
           //面,当我们返回将被销毁,所以停止了preVIEW。
           //因为CameraDevice对象不是共享资源,这是非常
           //重要的是要释放它当活动被暂停。
           mCamera.stop preVIEW();
           mCamera = NULL;
        }

        公共无效surfaceChanged(SurfaceHolder持有人,INT格式,INT W,INT高){
            //现在的尺寸是已知的,设置相机参数,并开始
            //在preVIEW。
            Camera.Parameters参数= mCamera.getParameters();
            parameters.set previewSize(W,H);
            mCamera.setParameters(参数);
            mCamera.start preVIEW();
       }
    }
 }
 

如果你运行它,你可以一见的preVIEW SurfaceView的形象,但我的问题是如何一起保存为JPEG格式的SD卡。您能否给一些建议,如果我需要格式转换,这样我可以将它们保存在同一时间。我的部分解决方案来保存图像唯一

  / **处理数据的JPEG图片* /
PictureCallback jpegCallback =新PictureCallback(){

公共无效onPictureTaken(byte []的数据,摄像头摄像头){
    FileOutputStream中outStream = NULL;
尝试 {
    //写入本地沙箱文件系统
        // outStream = CameraDemo.this.openFileOutput(的String.Format(%D.JPG
           System.currentTimeMillis的()),0);

        //或写入SD卡
    outStream =新的FileOutputStream(的String.Format(/ SD卡/%D.JPG
        System.currentTimeMillis的()));
    outStream.write(数据);
    outStream.close();
    Log.d(TAG,onPictureTaken  - 写字节:+ data.length);
}赶上(FileNotFoundException异常E){
    e.printStackTrace();
}赶上(IOException异常E){
    e.printStackTrace();
} 最后 {
}
Log.d(TAG,onPictureTaken  -  JPEG);
}
};
 

解决方案

我想你是问如何将两个图像合并成一个。

你应该做的第一件事就是用一个AsynchHandler保存你的形象,你不锁起来的用户界面,这不是你的问题的一部分,但是它会。

至于你的一般性问题是<一个href="http://stackoverflow.com/questions/5263152/save-image-overlay-with-camera-captured-image-underneith">asked并回答了这里一般你需要将两个图像部分合并到一起,处理您的覆盖的比例作为一个整体,或者分量图像部分。

I have a SurfaceView that is being used to draw images, and I would like to overlay them onto a live-feed from the phone's camera. I have read Overlay images onto camera preview surfaceview and how to draw an overlay on a surfaceview used by camera on android

    public class TestCameraOverlayActivity extends Activity { 
         Bitmap bitmap;

         /** Called when the activity is first created. */ 
         @Override 
         public void onCreate(Bundle savedInstanceState) { 
               super.onCreate(savedInstanceState); 
               requestWindowFeature(Window.FEATURE_NO_TITLE); 
               Preview mPreview = new Preview(this); 
               DrawOnTop mDraw = new DrawOnTop(this); 
               bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
               setContentView(mPreview); 
               addContentView(mDraw, new LayoutParams (LayoutParams.WRAP_CONTENT,
               LayoutParams.WRAP_CONTENT)); 
        } 

        class DrawOnTop extends View { 
            public DrawOnTop(Context context) { 
                super(context); 

        } 

        @Override 
        protected void onDraw(Canvas canvas) { 
            Paint paint = new Paint(); 
            paint.setStyle(Paint.Style.STROKE); 
            paint.setColor(Color.BLACK); 
            canvas.drawText("Test Text", 10, 10, paint); 
            canvas.drawBitmap(bitmap, 0, 0, null);
            super.onDraw(canvas); 
        } 
    } 

    class Preview extends SurfaceView implements SurfaceHolder.Callback { 
        SurfaceHolder mHolder; 
        Camera mCamera; 

        Preview(Context context) { 
            super(context); 
            // Install a SurfaceHolder.Callback so we get notified when the 
            // underlying surface is created and destroyed. 
            mHolder = getHolder(); 
            mHolder.addCallback(this); 
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        } 

        public void surfaceCreated(SurfaceHolder holder) { 
            // The Surface has been created, acquire the camera and tell it where 
            // to draw. 
            mCamera = Camera.open(); 
             try {
        mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {

            e.printStackTrace();
        } 
        } 

        public void surfaceDestroyed(SurfaceHolder holder) { 
           // Surface will be destroyed when we return, so stop the preview. 
           // Because the CameraDevice object is not a shared resource, it's very 
           // important to release it when the activity is paused. 
           mCamera.stopPreview(); 
           mCamera = null; 
        } 

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
            // Now that the size is known, set up the camera parameters and begin 
            // the preview. 
            Camera.Parameters parameters = mCamera.getParameters(); 
            parameters.setPreviewSize(w, h); 
            mCamera.setParameters(parameters); 
            mCamera.startPreview(); 
       } 
    } 
 }

if you run it you can one see image on the preview SurfaceView, but my question is how to save them together as jpeg on the sdcard. Can you give some advice, if i need Format conversion so that i can save them at the same time. My partial solution to save the image only

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {

public void onPictureTaken(byte[] data, Camera camera) {
    FileOutputStream outStream = null;
try {
    // write to local sandbox file system
        // outStream = CameraDemo.this.openFileOutput(String.format("%d.jpg", 
           System.currentTimeMillis()), 0); 

        // Or write to sdcard
    outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", 
        System.currentTimeMillis()));   
    outStream.write(data);
    outStream.close();
    Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};

解决方案

I think you are asking how to merge the two images into one.

The first thing you should do is use an AsynchHandler to save your image so you don't lock up the UI, that's not part of your issue but it will be.

As to your general question it was asked and answered here in general you will need to merge the two image parts together and handle scaling of your overlay as a whole, or the component image parts.

这篇关于图像叠加到摄像头preVIEW SurfaceView以及如何保存呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆