Android相机Camera基础知识
程序员文章站
2024-02-17 16:39:52
一、概述
本章节主要讲述的android相机camera的相关知识点,主要包含三个方面,android启动系统相机生成缩略图、android启动系统相机生成原图以及an...
一、概述
本章节主要讲述的android相机camera的相关知识点,主要包含三个方面,android启动系统相机生成缩略图、android启动系统相机生成原图以及android自定义相机等。
二、android启动系统相机
核心代码:
package com.czhappy.camerademo.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.net.uri; import android.os.bundle; import android.os.environment; import android.provider.mediastore; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.imageview; import com.czhappy.camerademo.r; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; public class mainactivity extends appcompatactivity { private static int req_1 = 1; private static int req_2 = 2; private imageview image_iv; private string mfilepath; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); image_iv = (imageview) findviewbyid(r.id.image_iv); mfilepath = environment.getexternalstoragedirectory().getpath(); mfilepath = mfilepath +"/" + system.currenttimemillis()+".jpg"; } public void startcamera1(view view){ //隐式intent调用 intent it = new intent(mediastore.action_image_capture); startactivityforresult(it, req_1); } public void startcamera2(view view){ //隐式intent调用 intent it = new intent(mediastore.action_image_capture); uri photouri = uri.fromfile(new file(mfilepath)); it.putextra(mediastore.extra_output, photouri); startactivityforresult(it, req_2); } public void customcamera(view view){ intent it = new intent(mainactivity.this, customcameraactivity.class); startactivity(it); } /** * * @param requestcode * @param resultcode * @param data //缩略图 */ @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode == result_ok){ if(requestcode == req_1){//缩略图 bundle bundle = data.getextras(); bitmap bmp = (bitmap) bundle.get("data"); image_iv.setimagebitmap(bmp); }else if(requestcode == req_2){//原图 fileinputstream fis = null; try { fis = new fileinputstream(mfilepath); bitmap bitmap = bitmapfactory.decodestream(fis); image_iv.setimagebitmap(bitmap); } catch (filenotfoundexception e) { e.printstacktrace(); }finally { try { fis.close(); } catch (ioexception e) { e.printstacktrace(); } } } } } }
三、android自定义相机
1.自定义相机步骤:
创建相机
创建surfaceview
关联相机与surfaceview
调整相机显示效果
自定义相机预览界面
2.核心代码:
自定义相机customcameraactivity.java
package com.czhappy.camerademo.activity; import android.content.intent; import android.graphics.imageformat; import android.hardware.camera; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import com.czhappy.camerademo.r; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; /** * description: * user: chenzheng * date: 2016/12/27 0027 * time: 13:43 */ public class customcameraactivity extends appcompatactivity implements surfaceholder.callback{ private surfaceview mpreview; private camera mcamera; private surfaceholder mholder; private camera.picturecallback mpicturecallback = new camera.picturecallback() { @override public void onpicturetaken(byte[] data, camera camera) { file tempfile = new file("/sdcard/temp.png"); try { fileoutputstream fos = new fileoutputstream(tempfile); fos.write(data); fos.close(); intent it = new intent(customcameraactivity.this, captureresultactivity.class); it.putextra("picpath", tempfile.getabsolutepath()); startactivity(it); finish(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_custom_camera); mpreview = (surfaceview) this.findviewbyid(r.id.preview); mholder = mpreview.getholder(); mholder.addcallback(this); //点击自动对焦 mpreview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mcamera.autofocus(null);; } }); } /** * 拍照 * @param view */ public void capture(view view){ camera.parameters parameters = mcamera.getparameters(); parameters.setpictureformat(imageformat.jpeg); parameters.setpicturesize(800,400); parameters.setfocusmode(camera.parameters.focus_mode_auto);//自动对焦 mcamera.autofocus(new camera.autofocuscallback() { @override public void onautofocus(boolean success, camera camera) { //对焦准确拍摄照片 if(success){ mcamera.takepicture(null, null, mpicturecallback); } } }); } /** * 获取相机对象 * @return */ private camera getcamera(){ mcamera = camera.open(); return mcamera; } /** * 开始预览相机内容 */ private void setstartpreview(camera camera, surfaceholder holder){ try { camera.setpreviewdisplay(holder); camera.setdisplayorientation(90);//设置相机预览为竖屏90度 camera.startpreview(); } catch (ioexception e) { e.printstacktrace(); } } /** * 释放相机资源 */ private void releasecamera(){ if(mcamera!=null){ mcamera.setpreviewcallback(null); mcamera.stoppreview(); mcamera.release(); mcamera = null; } } @override protected void onresume() { super.onresume(); if (mcamera==null){ mcamera = getcamera(); if(mholder!=null){ setstartpreview(mcamera, mholder); } } } @override protected void onpause() { super.onpause(); releasecamera(); } @override public void surfacecreated(surfaceholder holder) { setstartpreview(mcamera, mholder); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { mcamera.stoppreview(); setstartpreview(mcamera, mholder); } @override public void surfacedestroyed(surfaceholder holder) { releasecamera(); } }
生成图片结果页面captureresultactivity.java
package com.czhappy.camerademo.activity; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.matrix; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.widget.imageview; import com.czhappy.camerademo.r; import java.io.fileinputstream; import java.io.filenotfoundexception; /** * description: * user: chenzheng * date: 2016/12/30 0030 * time: 13:27 */ public class captureresultactivity extends appcompatactivity { private string picpath; private imageview result_iv; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_capture_result); picpath = getintent().getstringextra("picpath"); result_iv = (imageview) findviewbyid(r.id.result_iv); try { fileinputstream fis = new fileinputstream(picpath); bitmap bitmap = bitmapfactory.decodestream(fis); matrix matrix = new matrix(); matrix.setrotate(90);//旋转90度矩阵 bitmap = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, true); result_iv.setimagebitmap(bitmap); } catch (filenotfoundexception e) { e.printstacktrace(); } } }
四、项目源代码下载
http://xiazai.jb51.net/201701/yuanma/androidcamera(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android Glide的简单使用