main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_Flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SurfaceView
android:id="@+id/hiddenSurfaceView"
android:layout_width="1dp"
android:layout_height="1dp" />
</RelativeLayout>
MainActivity
package com.example;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
public Button Btn_Flashlight;
public static boolean hasFlash;
public static Camera mCamera;
public static Parameters mParameters;
public static PowerManager mPowerManager;
public int isLighOn =0;
public class MainActivity extends Activity implements OnClickListener,SurfaceHolder.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Btn_Flashlight = (Button) findViewById(R.id.btn_Flashlight);
hasFlash=getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
surfaceView = (SurfaceView) this.findViewById(R.id.hiddenSurfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
getCamera();
}
}
@Override
public void onClick(View v) {
if (v == Btn_Flashlight) {
if (hasFlash){
if (isLighOn==1){
turnOffFlash();
} else {
turnOnFlash();
}
} else {
Dialog();
}
}
}
private void getCamera() {
if (mCamera == null) {
try {
mCamera = Camera.open();
mParameters = mCamera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
private void turnOnFlash() {
if (!isFlashOn) {
if (mCamera == null || mParameters == null) {
return;
}
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(mParameters);
mCamera.startPreview();
isFlashOn = true;
}
}
private void turnOffFlash() {
if (isFlashOn) {
if (mCamera == null || mParameters == null) {
return;
}
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParameters);
mCamera.stopPreview();
isFlashOn = false;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
Constant.mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.e(TAG, "Unexpected IO Exception in setPreviewDisplay()", e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void Dialog () {
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
final AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert.show();
return;
}
}
AndroidManifest.xml
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:permissionGroup="android.permission-group.HARDWARE_CONTROLS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_Flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SurfaceView
android:id="@+id/hiddenSurfaceView"
android:layout_width="1dp"
android:layout_height="1dp" />
</RelativeLayout>
MainActivity
package com.example;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
public Button Btn_Flashlight;
public static boolean hasFlash;
public static Camera mCamera;
public static Parameters mParameters;
public static PowerManager mPowerManager;
public int isLighOn =0;
public class MainActivity extends Activity implements OnClickListener,SurfaceHolder.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Btn_Flashlight = (Button) findViewById(R.id.btn_Flashlight);
hasFlash=getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
surfaceView = (SurfaceView) this.findViewById(R.id.hiddenSurfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
getCamera();
}
}
@Override
public void onClick(View v) {
if (v == Btn_Flashlight) {
if (hasFlash){
if (isLighOn==1){
turnOffFlash();
} else {
turnOnFlash();
}
} else {
Dialog();
}
}
}
private void getCamera() {
if (mCamera == null) {
try {
mCamera = Camera.open();
mParameters = mCamera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
private void turnOnFlash() {
if (!isFlashOn) {
if (mCamera == null || mParameters == null) {
return;
}
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(mParameters);
mCamera.startPreview();
isFlashOn = true;
}
}
private void turnOffFlash() {
if (isFlashOn) {
if (mCamera == null || mParameters == null) {
return;
}
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParameters);
mCamera.stopPreview();
isFlashOn = false;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
Constant.mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.e(TAG, "Unexpected IO Exception in setPreviewDisplay()", e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void Dialog () {
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
final AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert.show();
return;
}
}
AndroidManifest.xml
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:permissionGroup="android.permission-group.HARDWARE_CONTROLS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

No comments:
Post a Comment