Hello friends,in this android example we learn how to write simple program for use camera flash as a torch light.
CREATE NEW ANDROID PROJECT
Step 1: write code into activity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:keepScreenOn="true" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="58dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
NOTE: Put image file into ...drawable/ 1)torchoff, 2)torchon
Step 2: Write code into MainActivity.java
package dev.Androidapplink.torchapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Context;
import android.view.View.OnClickListener;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean isFlashOn = false;
private Camera camera;
private ImageButton button;
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Torch start
button = (ImageButton) findViewById(R.id.button);
Context context = this;
PackageManager pm = context.getPackageManager();
//Check devise has a camera
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Camera.Parameters camparam = camera.getParameters();
if (camparam.getFlashMode() == null) {
Toast.makeText(getApplicationContext(),
R.string.flashlightstatus, Toast.LENGTH_LONG)
.show();
}
else {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isFlashOn = false;
button.setImageResource(R.drawable.torchoff);
} else {
Log.i("info", "torch is turned on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
button.setImageResource(R.drawable.torchon);
}
}
}
});
// Torch end
}
}
NOTE: Give permission in AndroidManifest.xml
its allow to access camera function.
<uses-permission android:name="android.permission.CAMERA" />
Step 3: Now Run Your Project:
No comments :
Post a Comment