您的位置:首页 > 移动开发 > Android开发

android 闪光灯编程

2016-07-16 19:50 267 查看
关于Camera的UI已经失效。

package com.example.flashlight;

import java.io.IOException;

import android.content.pm.PackageManager;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.TransitionDrawable;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

public class FlashLight extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

mImageViewFlashLight.setTag(false);

Point point = new Point();
getWindowManager().getDefaultDisplay().getSize(point);

LayoutParams layoutParams = (LayoutParams) mImageViewFlashLightController.getLayoutParams();
System.out.println(layoutParams.height);
System.out.println(layoutParams.width);
layoutParams.height = point.y * 3 / 4;
layoutParams.width = point.x * 1 / 3;
mImageViewFlashLightController.setLayoutParams(layoutParams);
}

public void onClick_flashlight(View view) {
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
Toast.makeText(this, "当前设备没有闪光灯", Toast.LENGTH_LONG).show();
return;
}
if ((Boolean) mImageViewFlashLight.getTag()==false) {
openFlashLight();
}else {
closeFlashLight();
}
}

protected void openFlashLight() {
TransitionDrawable drawable = (TransitionDrawable) mImageViewFlashLight.getDrawable();
drawable.startTransition(200);
mImageViewFlashLight.setTag(true);

mCamera = Camera.open();
int textureId = 0;
try {
mCamera.setPreviewTexture(new SurfaceTexture(textureId));
mCamera.startPreview();
mParameters = mCamera.getParameters();
mParameters.setFlashMode(mParameters.FLASH_MODE_TORCH);
mCamera.setParameters(mParameters);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

protected void closeFlashLight() {
TransitionDrawable drawable = (TransitionDrawable) mImageViewFlashLight.getDrawable();
if ((Boolean) mImageViewFlashLight.getTag()) {
drawable.reverseTransition(200);
mImageViewFlashLight.setTag(false);
if (mCamera != null) {
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParameters);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
closeFlashLight();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: