Android自定义相机实现自动对焦和手动对焦
程序员文章站
2024-02-26 17:41:10
android自定义相机实现自动对焦和手动对焦:
不调用系统相机,因为不同的机器打开相机呈现的界面不统一也不能满足需求。
所以为了让程序在不同的机器上呈现出统一的界面,...
android自定义相机实现自动对焦和手动对焦:
不调用系统相机,因为不同的机器打开相机呈现的界面不统一也不能满足需求。
所以为了让程序在不同的机器上呈现出统一的界面,并且可以根据需求进行布局,做了此demo。
程序实现代码如下:
import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.lang.reflect.method; import android.graphics.pixelformat; import android.hardware.camera; import android.hardware.camera.autofocuscallback; import android.hardware.camera.parameters; import android.hardware.camera.picturecallback; import android.hardware.camera.shuttercallback; import android.os.build; import android.os.bundle; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import com.hp.classes.config.constants; import com.hp.classes.tools.commonutils; import com.hp.classes.ui.baseactivity; @suppresswarnings("deprecation") public class photographactivity extends baseactivity implements onclicklistener, surfaceholder.callback { private surfaceview surfaceview; private camera camera; private camera.parameters parameters; private button btn_goback, btn_takephoto; private surfaceholder surfaceholder; @override protected void ondestroy() { super.ondestroy(); if(camera != null){ camera.stoppreview(); camera.release(); camera = null; } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.classes_activity_takephoto); initview(); } @override protected void onresume() { super.onresume(); initcamera(); } private void initview(){ btn_goback = (button) findviewbyid(r.id.btn_goback); btn_goback.setonclicklistener(this); btn_takephoto = (button) findviewbyid(r.id.btn_takephoto); btn_takephoto.setonclicklistener(this); surfaceview = (surfaceview) findviewbyid(r.id.surfaceview); surfaceview.setfocusable(true); surfaceview.setonclicklistener(this); surfaceview.setbackgroundcolor(trim_memory_background); surfaceholder = surfaceview.getholder(); surfaceholder.settype(surfaceholder.surface_type_push_buffers); surfaceholder.setkeepscreenon(true); surfaceholder.setfixedsize(400, 300); surfaceholder.addcallback(this); } @override public void surfacedestroyed(surfaceholder holder) { // todo auto-generated method stub camera.stoppreview(); camera.release(); camera = null; } @override public void surfacecreated(surfaceholder holder) { // todo auto-generated method stub try { camera.setpreviewdisplay(surfaceholder); } catch (ioexception e) { e.printstacktrace(); } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // 实现自动对焦 camera.autofocus(new autofocuscallback() { @override public void onautofocus(boolean success, camera camera) { if (success) { camera.cancelautofocus();// 只有加上了这一句,才会自动对焦 doautofocus(); } } }); } // 相机参数的初始化设置 private void initcamera() { if (null == camera) { camera = camera.open(); } parameters = camera.getparameters(); parameters.setpictureformat(pixelformat.jpeg); parameters.setflashmode(parameters.flash_mode_torch); if (!build.model.equals("koridy h30")) { parameters.setfocusmode(camera.parameters.focus_mode_continuous_picture);// 1连续对焦 }else{ parameters.setfocusmode(camera.parameters.focus_mode_auto); } camera.setparameters(parameters); setdispaly(camera); camera.startpreview(); camera.cancelautofocus();// 2如果要实现连续的自动对焦,这一句必须加上 } // 控制图像的正确显示方向 private void setdispaly(camera camera) { if (integer.parseint(build.version.sdk) >= 8) { setdisplayorientation(camera, -90); } else { parameters.setrotation(-90); } } // 实现的图像的正确显示 private void setdisplayorientation(camera camera, int i) { method downpolymorphic; try { downpolymorphic = camera.getclass().getmethod("setdisplayorientation", new class[] { int.class }); if (downpolymorphic != null) { downpolymorphic.invoke(camera, new object[] { i }); } } catch (exception e) { log.e("came_e", "图像出错"); } } @override public void onclick(view v) { switch (v.getid()) { case r.id.surfaceview: doautofocus(); break; case r.id.btn_takephoto: takepicture(); break; case r.id.btn_goback: finish(); break; default: break; } } // handle button auto focus private void doautofocus() { parameters = camera.getparameters(); parameters.setfocusmode(camera.parameters.focus_mode_auto); camera.setparameters(parameters); camera.autofocus(new autofocuscallback() { @override public void onautofocus(boolean success, camera camera) { if (success) { camera.cancelautofocus();// 只有加上了这一句,才会自动对焦。 if (!build.model.equals("koridy h30")) { parameters = camera.getparameters(); parameters.setfocusmode(camera.parameters.focus_mode_continuous_picture);// 1连续对焦 camera.setparameters(parameters); }else{ parameters = camera.getparameters(); parameters.setfocusmode(camera.parameters.focus_mode_auto); camera.setparameters(parameters); } } } }); } private void takepicture() { camera.takepicture(shuttercallback, rawcallback, jpegcallback); } // define shuttercallback shuttercallback shuttercallback = new shuttercallback() { public void onshutter() { // todo do something when the shutter closes. } }; picturecallback rawcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { // todo do something with the image raw data. } }; // stroe the picture in format jpeg picturecallback jpegcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { // save the image jpeg data to the sd card fileoutputstream outstream = null; try { //修改图片路径和名称 string tempfilename = string.valueof(system.currenttimemillis()) + ".jpg"; file folder = constants.cache_folder; if (!folder.isdirectory()) { folder.mkdirs(); } string path = constants.cache_folder + file.separator + tempfilename; outstream = new fileoutputstream(path); outstream.write(data); outstream.flush(); outstream.close(); surfaceview.setbackgrounddrawable(new bitmapdrawable(bitmapfactory.decodebytearray(data, 0, data.length))); } catch (filenotfoundexception e) { log.e("tag", "file note found", e); } catch (ioexception e) { log.e("tag", "io exception", e); } } }; }
classes_activity_takephoto.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <surfaceview android:id="@+id/surfaceview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_toleftof="@+id/rlright" /> <linearlayout android:id="@+id/rlright" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignparentright="true" android:background="#2b2b2b" android:gravity="center_horizontal" android:orientation="vertical" > <linearlayout android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="1" android:gravity="center" > <button android:id="@+id/btn_goback" style="@style/petextbutton" android:text="返回"/> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="1" android:gravity="center" > <button android:id="@+id/btn_takephoto" style="@style/petextbutton" android:text="拍照" /> </linearlayout> </linearlayout> </relativelayout>
在清单文件需要添加相应权限:
<uses-permission android:name="android.permission.camera"/> <uses-feature android:name="android.hardware.camera"/> <uses-feature android:name="android.hardware.camera.autofocus"/>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。