Android Camera实现毫秒级拍照实例
程序员文章站
2023-11-20 19:11:46
我们知道自定义camera需要以下几步
打开相机,即实例化camera对象,camera camera = camera.open();
设...
我们知道自定义camera需要以下几步
- 打开相机,即实例化camera对象,camera camera = camera.open();
- 设置camera的相关参数,camera.parameters parameters = camera.getparameters();
- 打开预览,camera.setpreviewdisplay(surfaceholder); camera.startpreview();
- 获取图片,这里只是从预览中获取因此使用,camera.setpreviewcallback(new camera.previewcallback(){……..});
- 停止预览,释放相机,camera.stoppreview();camera.release();
1、打开相机
public static camera openfacingbackcamera() { camera cam = null; camera.camerainfo camerainfo = new camera.camerainfo(); for (int camidx = 0, cameracount = camera.getnumberofcameras(); camidx < cameracount; camidx++) { camera.getcamerainfo(camidx, camerainfo); if (camerainfo.facing == camera.camerainfo.camera_facing_front) { try { //打开相应的摄像头,目前是前置摄像头 cam = camera.open(camidx); } catch (runtimeexception e) { e.printstacktrace(); if (cam!=null){ cam.stoppreview(); cam.release(); } cam = null; } } } return cam; }
2、设置相关参数和预览
//定义surfaceview private surfaceview localsurface; // 为surfaceholder添加一个回调监听器 localsurface.getholder().addcallback(new surfaceholder.callback() { @override public void surfacecreated(surfaceholder holder) { } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { camera = camerautil.openfacingbackcamera(); //设置相机预览旋转角度 camera.setdisplayorientation(180); if (camera != null) { camera.parameters parameters = camera.getparameters();// 获得相机参数 list<camera.size> sizes = parameters.getsupportedpreviewsizes(); list<camera.size> sizes_pic = parameters.getsupportedpicturesizes(); camera.size cs = sizes.get(0); int mwidth = cs.width; int mheight = cs.height; parameters.setpreviewsize(mwidth, mheight); parameters.setpicturesize(sizes_pic.get(0).width, sizes_pic.get(0).height); parameters.setjpegquality(85);// parameters.setfocusmode(camera.parameters.focus_mode_auto); parameters.setpictureformat(imageformat.jpeg); camera.setparameters(parameters); try { camera.setpreviewdisplay(localsurface.getholder()); camera.startpreview(); // 开始预览 camera.autofocus(null); // 自动对焦 } catch (ioexception e) { e.printstacktrace(); camera.stoppreview(); camera.release(); camera = null; } } } @override public void surfacedestroyed(surfaceholder holder) { if (camera!=null){ camera.stoppreview(); camera.release(); camera = null; } } } ); // 设置该surfaceview自己不维护缓冲 localsurface.getholder().settype(surfaceholder.surface_type_push_buffers);
3、获取图片
private void getviewimage() { //设置监听 camera.setpreviewcallback(new camera.previewcallback(){ @override public void onpreviewframe(byte[] data, camera camera) { camera.size size = camera.getparameters().getpreviewsize(); try{ yuvimage image = new yuvimage(data, imageformat.nv21, size.width, size.height, null); if(image!=null){ bytearrayoutputstream stream = new bytearrayoutputstream(); image.compresstojpeg(new rect(0, 0, size.width, size.height), 80, stream); bitmap bmp = bitmapfactory.decodebytearray(stream.tobytearray(), 0, stream.size()); //因为图片会放生旋转,因此要对图片进行旋转到和手机在一个方向上 rotatebitmap(bmp); stream.close(); } }catch(exception ex){ log.e("sys","error:"+ex.getmessage()); } } }); }
旋转图片的方法
public void rotatemybitmap(bitmap bmp){ matrix matrix = new matrix(); matrix.postscale(-1, 1); // 镜像水平翻转(如果左右颠倒) matrix.postrotate(180); bitmap nbmp = bitmap.createbitmap(bmp, 0,0, bmp.getwidth(), bmp.getheight(), matrix, true); imageview.setimagebitmap(nbmp); };
自定义时间拍照
private handler mhandler = new handler() { public void handlemessage(message msg) { switch (msg.what) { case 101: getviewimage(); log.e(tag, "handlemessage: 拍照" ); mhandler.sendemptymessagedelayed(102, 100); break; case 102: camera.setpreviewcallback(null); break ; } } };
然后点击按钮调用
mhandler.sendemptymessagedelayed(101, 100);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。