Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android to realize the function of simple flashlight

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use Android to implement a simple flashlight. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The details are as follows

XML:

Java:

Package com.example.flashlightactivite;import android.Manifest;import android.content.pm.PackageManager;import android.hardware.Camera;import android.os.Build;import android.support.v4.app.ActivityCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.widget.ImageButton;public class Switch_Activity extends AppCompatActivity {ImageButton flashLightSwitch; boolean flag = false; SurfaceView surfaceView; Camera camera; Camera.Parameters camera_parameters / / camera parameters / / set the resolution of the camera and the resolution supported by the device / / Camera.Size camera_size; SurfaceHolder surfaceHolder;// SurfaceHolder is an interface that acts like a listener for Surface. Provide methods related to accessing and controlling the Surface behind the SurfaceView / / * be sure to add permissions to the AndroidManifest.xml: / permission PERMISSION_STORAGE: allow storage private static String [] PERMISSION_STORAGE = {Manifest.permission.CAMERA}; / / request dynamic code REQUEST_PERMISSION_CODE: request permission code private static int REQUEST_PERMISSION_CODE = 1; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activitylicense switch _); / / determine the level of version number and lollipop version number if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {/ / checkSelfPermission: self-inspection permission if (ActivityCompat.checkSelfPermission (this,Manifest.permission.CAMERA)! = PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions (this,PERMISSION_STORAGE,REQUEST_PERMISSION_CODE);}} flashLightSwitch = findViewById (R.id.imageButton) SurfaceView = findViewById (R.id.surfaceView); camera = Camera.open (); camera_parameters = camera.getParameters (); surfaceHolder = surfaceView.getHolder (); surfaceHolder.addCallback (new SurfaceHolder.Callback () {@ Override public void surfaceCreated (SurfaceHolder holder) {try {camera.setPreviewDisplay (surfaceHolder);} catch (Exception e) {/ / release camera object camera.release ();} camera.startPreview () } @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {} @ Override public void surfaceDestroyed (SurfaceHolder holder) {}}); / ImageButton add click event flashLightSwitch.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (! flag) {/ / change the value of flag flag = true;// change picture R. Bag. Figure flashLightSwitch.setImageResource (R.drawable.on); / / sets the type flash mode of flash: torch camera_parameters.setFlashMode (Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters (camera_parameters);} else {flashLightSwitch.setImageResource (R.drawable.off); flag = false;// off flash camera_parameters.setFlashMode (Camera.Parameters.FLASH_MODE_OFF); camera.setParameters (camera_parameters) });}} / * * FLASH_MODE_RED_EYE red-eye prevention mode to reduce or prevent the red-eye appearance of portraits in the picture. FLASH_MODE_TORCH filling mode, which weakens the flash intensity in normal light. FLASH_MODE_AUTO automatic mode, which will flash automatically when necessary. FLASH_MODE_OFF flash mode will not be turned off when FLASH_MODE_ON snapshots will be turned off forever * * /

AndroidManifest.xml:

Thank you for reading! On "how to use Android to achieve simple flashlight function" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report