当前位置: 移动技术网 > IT编程>移动开发>Android > Android Camera开发手电筒功能

Android Camera开发手电筒功能

2019年07月24日  | 移动技术网IT编程  | 我要评论

天使寻羽毛,秋天 教案,qq豪杰社区

这是一个简单的运用android camera开发手电筒功能,androidmanifest.xml文件的入口是startapp,这个文件没上传上来,大家可以自己写。

flashlight.java

package com.android.app;

import android.app.activity;
import android.hardware.camera;
import android.hardware.camera.parameters;
import android.os.bundle;
import android.view.view;
import android.widget.textview;
import android.widget.toast; 

public class main extends activity {
 private boolean isopent = false;
 private camera camera; 

 @override

 protected void oncreate(bundle savedinstancestate) {
  // todo auto-generated method stub
  super.oncreate(savedinstancestate);
  view view = view.inflate(this, r.layout.main, null);
  setcontentview(view);
  textview img_but = (textview) findviewbyid(r.id.main_img);
  img_but.setonclicklistener(new view.onclicklistener() {

   @override
   public void onclick(view v) {
    // todo auto-generated method stub
    if (!isopent) {
     toast.maketext(getapplicationcontext(), "您已经打开了手电筒", 0)
       .show();
     camera = camera.open();
     parameters params = camera.getparameters();
     params.setflashmode(parameters.flash_mode_torch);
     camera.setparameters(params);
     camera.startpreview(); // 开始亮灯

     isopent = true;

    } else {
     toast.maketext(getapplicationcontext(), "关闭了手电筒",
       toast.length_short).show();
     camera.stoppreview(); // 关掉亮灯
     camera.release(); // 关掉照相机
     isopent = false;

    }

   }

  });

 }

 

}

布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a>
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 
 <textview
  android:id="@+id/main_img"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/main_body">
 </textview>

</linearlayout>

androidmanifest.xml文件

<manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a>
 package="com.android.app"
 android:versioncode="1"
 android:versionname="1.0" > 

 <uses-sdk
  android:minsdkversion="8"
  android:targetsdkversion="15" />
 <application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/apptheme" >
  <activity android:name=".appstart" >
   <intent-filter>
    <action android:name="android.intent.action.main" />

 

    <category android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>
  <activity android:name=".main" >
  </activity>
 </application>
 <!-- 摄像头、手电筒 -->
 <uses-permission android:name="android.permission.camera" />
 <uses-permission android:name="android.permission.flashlight" />

 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
 <uses-feature android:name="android.hardware.camera.flash" />
 

</manifest>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网