手电筒软件测试初学者,新手学习android做得一个闪光灯手电筒(测试过很多机型都可以)…

看到很多刚学习的同志都很纠结按照 上很多代码的实现方法都打开不了闪光灯,确实刚开始也有同感滴啦!希望能给你们一些思路!

废话不多说先上几张图片(有图有真像哦!!!!!!!)

思路:开启闪光灯的方法

我是这样做的

package com.cai.helloworld;

import java.io.IOException;

import android.content.Context;

import android.hardware.Camera;

import android.util.Log;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements

SurfaceHolder.Callback {

private Camera mCamera;

private SurfaceHolder mHolder;

public static final String TAG = “CameraPreview”;

public CameraPreview(Context context) {

super(context);

}

@SuppressWarnings(“deprecation”)

public CameraPreview(Context context, Camera camera) {

super(context);

mCamera = camera;

mHolder = getHolder();

mHolder.addCallback(this);

// deprecated setting, but required on Android versions prior to 3.0

mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

// The Surface has been created, now tell the camera where to draw the

// preview.

try {

mCamera.setPreviewDisplay(holder);

mCamera.startPreview();

} catch (IOException e) {

Log.d(TAG, “Error setting camera preview: ” + e.getMessage());

}

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// If your preview can change or rotate, take care of those events here.

// Make sure to stop the preview before resizing or reformatting it.

if (mHolder.getSurface() == null) {

// preview surface does not exist

return;

}

// stop preview before making changes

try {

mCamera.stopPreview();

} catch (Exception e) {

// ignore: tried to stop a non-existent preview

}

// set preview size and make any resize, rotate or

// reformatting changes here

// start preview with new settings

try {

mCamera.setPreviewDisplay(mHolder);

mCamera.startPreview();

} catch (Exception e) {

Log.d(TAG, “Error starting camera preview: ” + e.getMessage());

}

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

}

}

获取CameraPreview后就可以对闪光灯进行操作

下面就是手电筒Demo的例子:

package com.cai.helloworld;

import java.util.List;

import android.hardware.Camera;

import android.hardware.Camera.Parameters;

import android.os.Bundle;

import android.provider.Settings;

import android.app.Activity;

import android.content.ContentResolver;

import android.content.Context;

import android.content.pm.PackageManager;

import android.util.Log;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.widget.FrameLayout;

import android.widget.ImageButton;

import android.widget.Toast;

public class MainActivity extends Activity {

protected static final String TAG = “>>>>>>>MainActivity”;

private Camera mCamera;

private CameraPreview mPreview;

private FrameLayout preview;

private boolean isopent;

private boolean isCameraExist;

private ImageButton captureButton;

private int defualtLigth;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

defualtLigth = getScreenBrightness(this);

isCameraExist = checkCameraHardware(this);

if (isCameraExist) {

controlFlashLigth();

} else {

Toast.makeText(this, “您的设备没有照相功能或者此功能不可用”, Toast.LENGTH_LONG)

.show();

}

}

public void controlFlashLigth() {

// Create an instance of Camera

captureButton = (ImageButton) findViewById(R.id.button_capture);

mCamera = getCameraInstance();

// Create our Preview view and set it as the content of our

// activity.

mPreview = new CameraPreview(this, mCamera);

preview = (FrameLayout) findViewById(R.id.camera_preview);

preview.addView(mPreview);

preview.setVisibility(View.INVISIBLE);

captureButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (!isopent) {

v.setBackgroundResource(R.drawable.flashlight_on);

Toast.makeText(getApplicationContext(), “手电筒已开启”,

Toast.LENGTH_SHORT)

.show();

turnLightOn(mCamera);

isopent = true;

} else {

v.setBackgroundResource(R.drawable.flashlight_off);

Toast.makeText(getApplicationContext(), “手电筒已关闭”,

Toast.LENGTH_SHORT).show();

turnLightOFF(mCamera);

isopent = false;

}

}

});

}

/** A safe way to get an instance of the Camera object. */

public static Camera getCameraInstance() {

Camera c = null;

try {

c = Camera.open(); // attempt to get a Camera instance

} catch (Exception e) {

// Camera is not available (in use or does not exist)

}

return c; // returns null if camera is unavailable

}

/** Check if this device has a camera */

public boolean checkCameraHardware(Context context) {

if (context.getPackageManager().hasSystemFeature(

PackageManager.FEATURE_CAMERA)) {

// this device has a camera

return true;

} else {

// no camera on this device

return false;

}

}

@Override

protected void onResume() {

super.onResume();

Log.e(TAG, “onResume>>>>>runtime”);

}

@Override

protected void onPause() {

super.onPause();

Log.e(TAG, “onPause>>>>>runtime”);

}

@Override

protected void onDestroy() {

super.onDestroy();

Log.e(TAG, “onDestroy>>>>>runtime”);

releaseCamera();

}

@Override

protected void onStop() {

super.onStop();

Log.e(TAG, “HomeANXIALE>>>onStop>>>>>runtime”);

}

private void releaseCamera() {

if (mCamera != null) {

mCamera.release(); // release the camera for other applications

mCamera = null;

}

}

public void turnLightOn(Camera mCamera) {

if (mCamera == null) {

return;

}

Parameters parameters = mCamera.getParameters();

if (parameters == null) {

return;

}

List flashModes = parameters.getSupportedFlashModes();

// Check if camera flash exists

if (flashModes == null) {

// Use the screen as a flashlight (next best thing)

captureButton.setBackgroundColor(this.getResources().getColor(

R.color.white));

setBrightness(this, 255);

return;

}

String flashMode = parameters.getFlashMode();

if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {

// Turn on the flash

if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {

parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);

mCamera.setParameters(parameters);

} else {

}

}

}

public void turnLightOFF(Camera mCamera) {

if (mCamera == null) {

return;

}

Parameters parameters = mCamera.getParameters();

if (parameters == null) {

return;

}

List flashModes = parameters.getSupportedFlashModes();

// Check if camera flash exists

if (flashModes == null) {

setBrightness(this, defualtLigth);

// Use the screen as a flashlight (next best thing)

return;

}

String flashMode = parameters.getFlashMode();

if (!flashMode.equals(Parameters.FLASH_MODE_OFF)) {

// Turn on the flash

if (flashModes.contains(Parameters.FLASH_MODE_OFF)) {

parameters.setFlashMode(Parameters.FLASH_MODE_OFF);

mCamera.setParameters(parameters);

} else {

}

}

}

/**

* 获取屏幕的亮度

*

* @param activity

* @return

*/

public static int getScreenBrightness(Activity activity) {

int nowBrightnessValue = 0;

ContentResolver resolver = activity.getContentResolver();

try {

nowBrightnessValue = android.provider.Settings.System.getInt(

resolver, Settings.System.SCREEN_BRIGHTNESS);

} catch (Exception e) {

e.printStackTrace();

}

return nowBrightnessValue;

}

/**

* 设置屏幕的亮度

*

* @param activity

* @return

*/

public static void setBrightness(Activity activity, int brightness) {

// Settings.System.putInt(activity.getContentResolver(),

// Settings.System.SCREEN_BRIGHTNESS_MODE,

// Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

WindowManager.LayoutParams lp = activity.getWindow().getAttributes();

lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);

// lp.screenBrightness =(brightness) * (1f / 255f);

activity.getWindow().setAttributes(lp);

}

接下来就是新建一个布局文件activity_main.xml

布局中用到Framelayout,首先camera_preview用于获取camerapreview,之后用ImageButton对其进行覆盖。

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:orientation=”vertical” >

android:id=”@+id/camera_preview”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent” />

android:id=”@+id/button_capture”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:background=”@drawable/flashlight_off”

android:layout_gravity=”center” />

第一次写这个也不知道要说些什么,有什么看不懂的可以留言提问,会及时回答的

相关资源:专业LED灯光动画制作软件(安装后直接用!)_setup安装包-Delphi工具…

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年6月26日
下一篇 2021年6月26日

相关推荐