Android Camera Development Flashlight function _android

Source: Internet
Author: User
Tags stub

This is a simple use of Android camera development flashlight function, Androidmanifest.xml file entrance is Startapp, this file is not uploaded, you can write.

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 (sav
  Edinstancestate);
  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-ge
     nerated method Stub if (!isopent) {Toast.maketext (Getapplicationcontext (), "You have turned on the flashlight", 0). Show ();
     Camera = Camera.open ();
     Parameters params = Camera.getparameters ();
  Params.setflashmode (Parameters.flash_mode_torch);   Camera.setparameters (params); Camera.startpreview ();

    Start light isopent = true;
     else {Toast.maketext (Getapplicationcontext (), "Turn off Flashlight", Toast.length_short). Show (); Camera.stoppreview (); Turn off the light camera.release ();

    Turn off the camera isopent = false;

 }

   }

  });

 }

 

}

Layout file Code

<?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"
  Fill_parent "
  android:layout_height=" fill_parent "
  android:background=" @drawable/main_body ">
 </TextView>

</LinearLayout>

Androidmanifest.xml file

<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= ""/> <appl
  Ication 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 "/> <categor Y android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity Andro Id:name= ". Main "> </activity> </application> <!--camera, flashlight--> <uses-permission android:name=" android.pe Rmission. 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>

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.