标签:照相机
重点:
1、SurfaceView, OpenGL ES 的纹理------照相机捕获图像流纹理
2、style.xml配置文件。设置无标题等信息
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="android:windowBackground">@drawable/bg</item> </style>
3、使用FrameLayout布局,<include/>标签引入子布局。
main.xml
<p><FrameLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" xmlns:tools="<a target=_blank href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" > <include layout="@layout/mui_flashlight"/></p><p></FrameLayout></p>
ui_flashlight.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mui_flashlight" > <ImageView android:id="@+id/img_flashlight" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/flashlight" /> <ImageView android:id="@+id/img_flashlight_controller" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="bottom|center_horizontal" android:onClick="onClick_Flashlight" /> </FrameLayout>
4、使用flash.xml文件实现渐变效果,然后利用android:src="@drawable/flashlight",引入。
flash.xml
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/off"/> <item android:drawable="@drawable/on"/> </transition>
5、功能代码
Flashlight.java
public class MyFlashLight extends MyBaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); iView.setTag(false); //获取屏幕的尺寸 Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); LayoutParams layoutParams = (LayoutParams) iViewController.getLayoutParams(); //设置热点区的范围 layoutParams.height = point.y * 3/4; layoutParams.width = point.x /3; iViewController.setLayoutParams(layoutParams); } public void onClick_Flashlight(View view){ if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) { Toast.makeText(this, "当前设备没有闪光灯", Toast.LENGTH_SHORT).show(); return; } if (((Boolean)iView.getTag()) == false) { openFlashlight(); } else { closeFlashlight(); } } private void openFlashlight() { //TransitionDrawable是一个特殊的Drawable对象,可以实现两个drawable资源之间淡入淡出的效果 TransitionDrawable tDrawable = (TransitionDrawable) iView.getDrawable(); //切换时间200毫秒 tDrawable.startTransition(200); iView.setTag(true); try { //打开相机 mCamera = Camera.open(); int texturId = 0; //预览纹理默认给个值 即可 //照相机获取图像流纹理 mCamera.setPreviewTexture(new SurfaceTexture(texturId)); //启动预览 mCamera.startPreview(); //使用getParameters()获取Camera 的缺省配置参数 mParameters = mCamera.getParameters(); //设置闪光灯模式 mParameters.setFlashMode(mParameters.FLASH_MODE_TORCH); mCamera.setParameters(mParameters); } catch (IOException e) { e.printStackTrace(); } } private void closeFlashlight() { TransitionDrawable tDrawable2 = (TransitionDrawable) iView.getDrawable(); if ((Boolean) iView.getTag()) { //倒过来时间切换 tDrawable2.reverseTransition(200); iView.setTag(false); if (mCamera != null) { //使用getParameters()获取Camera 的缺省配置参数 mParameters = mCamera.getParameters(); //设置闪光灯模式 mParameters.setFlashMode(Parameters.FLASH_MODE_OFF); mCamera.setParameters(mParameters); //停止预览 mCamera.stopPreview(); //释放相机对象 mCamera.release(); //为了使垃圾回收器尽快回收 mCamera = null; } } } //退出程序关闭闪光灯 @Override protected void onPause() { super.onPause(); closeFlashlight(); } }
完整代码见附件(进入我的资料即可下载):MyFlashLight.zip
实用代码参考:
http://blog.csdn.net/dclchj/article/details/7421778
http://www.cnblogs.com/over140/archive/2012/09/26/2611999.html
标签:照相机
原文地址:http://xiaoshitou.blog.51cto.com/6396736/1626621