android实现手机App实现拍照功能示例
程序员文章站
2023-12-18 19:51:16
实现手机app实现拍照功能结果如下
第一步:
activity_takephoto.xml布局用surfaceview
实现手机app实现拍照功能结果如下
第一步:
activity_takephoto.xml布局用surfaceview
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_takephoto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.android_27.takephotoactivity"> <surfaceview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/sv_takephoto" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" android:id="@+id/b_ok" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="切换" android:id="@+id/b_qh" /> </linearlayout> </relativelayout>
第二步:takephotoactivity类
public class takephotoactivity extends appcompatactivity { private surfaceview sv_takephoto; private button b_ok; private button b_qh; private camera camera; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_takephoto); //获得surfaceview的id sv_takephoto =(surfaceview) findviewbyid(r.id.sv_takephoto); //获得拍照的id b_ok =(button)findviewbyid(r.id.b_ok); //获得切换镜头的id b_qh =(button)findviewbyid(r.id.b_qh); sv_takephoto.getholder().addcallback(new surfaceholder.callback() { @override public void surfacecreated(surfaceholder surfaceholder) { //打开相机 camera = camera.open(); //给相机设置参数 camera.parameters parameters= camera.getparameters(); //设置保存的格式 parameters.setpictureformat(pixelformat.jpeg); parameters.set("jpeg-quality",85); camera.setparameters(parameters); //将画面展示到surfaceview中 try { camera.setpreviewdisplay(sv_takephoto.getholder()); //开启预览 camera.startpreview(); } catch (ioexception e) { e.printstacktrace(); } } @override public void surfacechanged(surfaceholder surfaceholder, int i, int i1, int i2) { } @override public void surfacedestroyed(surfaceholder surfaceholder) { } }); //拍照的点击事件 b_ok.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //拍照 camera.takepicture(null, null, new camera.picturecallback() { @override public void onpicturetaken(byte[] bytes, camera camera) { //将字节数组转成图片 bitmap bitmap= bitmapfactory.decodebytearray(bytes,0,bytes.length); try { fileoutputstream fos=new fileoutputstream("/mnt/sdcard/dcim/"+system.currenttimemillis()+".jpg"); bitmap.compress(bitmap.compressformat.png,85,fos); camera.stoppreview(); camera.startpreview(); } catch (filenotfoundexception e) { e.printstacktrace(); } } }); } }); } }
第三步:在androidmanifest.xml中加权限
<uses-permission android:name="android.permission.camera" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。