当前位置: 代码迷 >> Android >> Camera类怎么拍照并返回byte[]
  详细解决方案

Camera类怎么拍照并返回byte[]

热度:107   发布时间:2016-05-01 14:57:39.0
Camera类如何拍照并返回byte[]
Camera类如何拍照并返回byte[]?


------解决方案--------------------
Camera.PictureCallback中的onPictureTaken(byte[] data, Camera camera),第一个参数就是图片数据
------解决方案--------------------
就是一个回调函数啊
------解决方案--------------------
Java code
/*** 拍照的回调接口*/PictureCallback mPictureCallback = new PictureCallback() {@Overridepublic void onPictureTaken(byte[] data, Camera camera) {Log.v(“PictureCallback”, “…onPictureTaken…”);if (data != null) {Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);mImageView.setImageBitmap(bitmap);mImageView.setVisibility(View.VISIBLE);mSurfaceView.setVisibility(View.GONE);if (mPreviewRunning) {mCamera.stopPreview();mPreviewRunning = false;}}}};本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiongyingzhuantu/archive/2010/12/11/6069454.aspx
------解决方案--------------------
探讨

Java code
/**
* 拍照的回调接口
*/
PictureCallback mPictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.v(“PictureCallback”, “…onPictureTaken…”);……

------解决方案--------------------
有没有startPreview
------解决方案--------------------
返回的那个byte[] 如何转化成bitmap啊 ? 那个byte里的数据时如何存放的啊 ? 有什么规律呢 ?
比如一个像素点是顺序排放在byte[1] byte[2] byte[3] 这样排放的吗 ?
------解决方案--------------------
你把下面这个类设置成Activity的ContentView
Java code
/**     * The camera preview control.     */    class Preview extends SurfaceView implements SurfaceHolder.Callback     {        /**         * Display surface holder.         */        private SurfaceHolder m_holder;        /**         * The camera object.         */        private Camera        m_camera;        /**         * The process running context.         */        private TakePhoto     m_context;        /**         * Constructor.         *          * @param context         */        Preview(Context context)         {            super(context);            m_context = (TakePhoto) context;            m_holder = getHolder();            m_holder.addCallback(this);            m_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        }        /**         * This is called immediately after the surface is first created.         *          * @param holder   The surface to display.         */        @Override        public void surfaceCreated(SurfaceHolder holder)         {            // start camera.            m_camera = Camera.open();            try             {                m_camera.setPreviewDisplay(holder);            }             catch (IOException exception)             {                m_camera.release();                m_camera = null;            }        }        /**         * This is called immediately before a surface is being destroyed.         *          * @param holder   The surface to destroy.         */        @Override        public void surfaceDestroyed(SurfaceHolder holder)         {            m_camera.stopPreview();            m_camera.release();            m_camera = null;        }        /**         * This is called immediately after any structural changes (format or size)          * have been made to the surface.         *          * @param holder   The surface to display.         *        format   The format of the picture.         *        w        The width of the holder.         *        h        The height of the holder.         */        @Override        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)         {            Camera.Parameters parameters = m_camera.getParameters();            parameters.setPictureFormat(PixelFormat.JPEG);            parameters.setPreviewSize(getWidth(), getHeight());            m_camera.setParameters(parameters);            m_camera.startPreview();        }        /**         * Taking photo.         */        public void takePicture()         {            if (m_camera != null)             {                m_camera.takePicture(null, null, jpegCallback);            }        }        /**         * Output photos after taking.         */        private PictureCallback jpegCallback = new PictureCallback()         {            public void onPictureTaken(byte[] _data, Camera _camera)             {                 [color=#FF0000]// 这里的_data就是你要的图片数据啦[/color]            }        };    }
  相关解决方案