Android实现手机拍照功能
程序员文章站
2023-12-13 18:25:52
本文实例为大家讲解如何轻松实现android手机拍照功能,分享给大家供大家参考。具体如下:
一、布局文件main.xml
本文实例为大家讲解如何轻松实现android手机拍照功能,分享给大家供大家参考。具体如下:
一、布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <surfaceview android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <relativelayout android:id="@+id/buttonlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" > <button android:id="@+id/takepicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_marginright="5dp" android:onclick="takepicture" android:text="@string/takepicture" /> <button android:id="@+id/autofocus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@id/takepicture" android:layout_marginright="20dp" android:layout_toleftof="@id/takepicture" android:onclick="takepicture" android:text="@string/autofocus" /> </relativelayout> </framelayout>
二、mainactivity.java
import java.io.file; import java.io.fileoutputstream; import android.app.activity; import android.hardware.camera; import android.hardware.camera.picturecallback; import android.os.bundle; import android.os.environment; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceholder.callback; import android.view.surfaceview; import android.view.view; import android.view.viewgroup; import android.view.window; import android.view.windowmanager; public class mainactivity extends activity { private view layout; private camera camera; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.main); layout = this.findviewbyid(r.id.buttonlayout); surfaceview surfaceview = (surfaceview) this .findviewbyid(r.id.surfaceview); surfaceview.getholder() .settype(surfaceholder.surface_type_push_buffers); surfaceview.getholder().setfixedsize(176, 144); surfaceview.getholder().setkeepscreenon(true); surfaceview.getholder().addcallback(new surfacecallback()); } public void takepicture(view v) { if (camera != null) { switch (v.getid()) { case r.id.takepicture: camera.takepicture(null, null, new mypicturecallback()); break; case r.id.autofocus: camera.autofocus(null); break; } } } private final class mypicturecallback implements picturecallback { public void onpicturetaken(byte[] data, camera camera) { try { file jpgfile = new file( environment.getexternalstoragedirectory(), system.currenttimemillis() + ".jpg"); fileoutputstream outstream = new fileoutputstream(jpgfile); outstream.write(data); outstream.close(); camera.startpreview(); } catch (exception e) { e.printstacktrace(); } } } private final class surfacecallback implements callback { public void surfacecreated(surfaceholder holder) { try { camera = camera.open();// 打开摄像头 camera.parameters parameters = camera.getparameters(); // log.i("mainactivity", parameters.flatten()); parameters.setpreviewsize(800, 480); parameters.setpreviewframerate(5); parameters.setpicturesize(1024, 768); parameters.setjpegquality(80); camera.setparameters(parameters); camera.setpreviewdisplay(holder); camera.startpreview();// 开始预览 } catch (exception e) { e.printstacktrace(); } } public void surfacechanged(surfaceholder holder, int format, int width, int height) { } public void surfacedestroyed(surfaceholder holder) { if (camera != null) { camera.release(); camera = null; } } } @override public boolean ontouchevent(motionevent event) { if (event.getaction() == motionevent.action_down) { layout.setvisibility(viewgroup.visible); return true; } return super.ontouchevent(event); } }
三、添加权限
<uses-permission android:name="android.permission.camera" /> <!-- 在sdcard中创建与删除文件权限 --> <uses-permission android:name="android.permission.mount_unmount_filesystems" /> <!-- 往sdcard写入数据权限 --> <uses-permission android:name="android.permission.write_external_storage" />
效果如下:
希望本文所述对大家学习android软件编程有所帮助。