Getting the torch to glow? - android

My objective is as simple as to have my own flashlight app; I have researched both the android developer site and stackoverflow quite thoroughly but seem to lack the connecting bits:
package my.torch;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.CompoundButton;
public class MainActivity extends Activity {
/* 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
}
*/
Camera mCamera;
CompoundButton mTorch;
Parameters camParams;
private Context context;
AlertDialog.Builder builder;
AlertDialog alertDialog;
public SurfaceView surfaceView;
public SurfaceHolder surfaceHolder;
public String flashMode = null;
/** A safe way to get an instance of the Camera object. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.mysurface);
surfaceHolder = surfaceView.getHolder();
context = MainActivity.this;
if(context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)){
mTorch = (CompoundButton) findViewById(R.id.toggleButton1);
}
else{
// should use Dialog
// tell the user that he has no camera
//showDialog(context, FLASH_NOT_SUPPORTED);
}
final Camera mCamera = Camera.open(0);
mTorch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.e("mTorch", "on");
Parameters camParams = mCamera.getParameters();
// params.setFlashMode( Parameters.FLASH_MODE_OFF )
camParams.setFlashMode( Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(camParams);
mCamera.startPreview();
flashMode = camParams.getFlashMode();
Log.e("mTorch", flashMode );
// The toggle is enabled
} else {
// The toggle is disabled
Log.e("mTorch", "off");
Parameters camParams = mCamera.getParameters();
// params.setFlashMode( Parameters.FLASH_MODE_OFF )
camParams.setFlashMode( Parameters.FLASH_MODE_OFF);
mCamera.setParameters(camParams);
mCamera.stopPreview();
flashMode = camParams.getFlashMode();
Log.e("mTorch", flashMode );
}
}
});
//mCamera.release();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onDestroy() {
super.onDestroy();
if (mCamera != null) {
camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(camParams);
mCamera.stopPreview();
mCamera.release();
}
}
}
The logging happily reports that the flashmode is torch or off as intended, however: The flash does not torch at all.
I read that I needed a surfaceview so I added it but with 0 height.
As I did collect bits and pieces here and there the code is probably not beautiful, my apologies.
Any idea on what I'm missing out?
(this is for a Nexus 5, 4.4.2)

Did you add permission to access camera in your manifest file?
uses-permission android:name="android.permission.CAMERA"

Related

Why my android code is of large size?

Following is the code for my app for a flash light
Its size comes out to be 1.4 MB which is too huge .
So i used progaurd to reduce size which ends up in 750 KB which is still huge as compared to code i am using.
Why the size is large ? and how to reduce it ? or i have made some mistake
package com.example.torch;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button power;
int flag=0;
public Camera camera;
private boolean hasFlash;
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onStop() {
super.onStop();
try{
}catch (Exception e){
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
power=(Button)findViewById(R.id.button1);
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.show();
return;
}
camera = Camera.open();
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
flag = 1;
power.setText("Power On");
power.setBackgroundColor(Color.BLUE);
power.setTextColor(Color.WHITE);
power.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if (flag==0) {
camera = Camera.open();
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
flag = 1;
power.setText("Power On");
power.setBackgroundColor(Color.BLUE);
}
else
{
camera.stopPreview();
camera.release();
camera = null;
flag=0;
power.setText("Power Off");
power.setBackgroundColor(Color.BLACK);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
You are extending AppCompatActivity, this means that your project includes Android Support Library v7, if you don't care about material look on old Android versions, you can simply remove it. Check your build.gradle file to see which libraries are included as those libraries will make your apk file bigger.
Other causes of a large apk file might be:
Drawables
Assets

Preview of face camera using surface view android

I have the following java code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import com.testing.CameraOperations;
public class MainActivity extends Activity {
private SurfaceView surface;
private CameraOperations camera;
private SoundOperations sound;
private static boolean woken = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#SuppressWarnings("unused")
public void test(View v){
if(!woken){
surface = (SurfaceView)findViewById(R.id.cameraView);
SurfaceHolder holder = surface.getHolder();
surface.setCamera(camera.toggleFaceCamera());
Animation out = AnimationUtils.makeOutAnimation(this, true);
v.startAnimation(out);
v.setVisibility(View.INVISIBLE);
woken = true;
}
}
}
And another class cameraOperations:
import android.hardware.Camera;
public class CameraOperations{
private Camera cam = null;
private Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
private static int camNumber = Camera.getNumberOfCameras();
public Camera toggleFaceCamera(){
if(cam == null){
for(int counter = 0; counter < camNumber; counter++){
Camera.getCameraInfo(counter, cameraInfo);
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
try{
cam = Camera.open(counter);
return cam;
}catch(RuntimeException e){return null;}
}
}
}
return null;
}
}
The function "test" is ran by button onclick and the animation works just fine. I've read a bunch of tutorials and references to this BUT I haven't been able to successfully preview the camera to the user. If someone could actually make me understand clearly and by coding the relation between the surface, the holder and the camera preview I would be much thankful!

FLASH_MODE_TORCH working/not working

I wanted to build a flashlight app using the following code.
It's working on a friend's HTC Desire HD, but it isn't on my RAZR and a friend's Galaxy Nexus.
I also tried the solution with focus_mode_infinity, but there's still no success.
package com.example.flashlight;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Camera camera = null;
Parameters parameters;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button OnOff = (Button)findViewById(R.id.Switch);
OnOff.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
if(camera == null) {
camera = Camera.open();
camera.startPreview();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
camera.setParameters(parameters);
}
else {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(parameters);
camera.release();
camera = null;
}
}
});
}}
I think FLASH_MODE_TORCH is not supported by RAZR, I've had the same issue with a customer reporting the same problem for one app (Flash not flashing).
What I suggest you is before setting the parameter to check if its supported:
List<String> flashModes = parameters.getSupportedFlashModes();
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
// Mode supported good to go
}
after setting parameters of the camera use the below method:
camera.startPreview();
here camera is your Camera object.

Android - Getting camera LED to strobe with handler?

I am using a timed alert (working fine) to try and run code that will quickly switch camera flash LED between off and on positions.
I'm trying to use a handler to switch between the two modes but cannot seem to get it to run.
I was wondering if someone could suggest another way to strobe the camera LED or if they could try to find something wrong with my code.
Help would be greatly appreciated, I'm really new to android programming.
Here's the code:
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
public class MyAlert extends Activity {
private Handler mHander = new Handler();
private boolean mActive = false;
private boolean mSwap = true;
private Camera camera;
final Parameters p = camera.getParameters();
private final Runnable mRunnable = new Runnable() {
public void run() {
if (mActive) {
if (mSwap) {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera = Camera.open();
camera.setParameters(p);
camera.startPreview();
mSwap = false;
mHander.postDelayed(mRunnable, 20);
} else {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera = Camera.open();
camera.setParameters(p);
camera.stopPreview();
mSwap = true;
mHander.postDelayed(mRunnable, 100);
}
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startStrobe();
}
private void startStrobe() {
mActive = true;
mHander.post(mRunnable);
}
};
AFAIR, stopping preview and releasing the camera is enough to switch flash off. But real problem is that devices behave differently and not always as advertised.

Access Device hardware (Camera Flash) Programically ina android

I have one question that can we access android device camera's flash light
programmically.Is this possible to turn on and off flash light programmically?
please give me my answer .
thanks in advance.
package com.thedevelopersinfo.tutorial.android.soundrecordingexample;
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
private MediaRecorder mediaRecorder;
private File file = null;
static final String PREFIX = "record";
static final String EXTENSION = ".3gpp";
private Button b1;
Camera mCamera;
public boolean isOn=true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
b1=(Button)findViewById(R.id.startBtn);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setFlashlight();
}
});
}
public void setFlashlight()
{
if (mCamera == null)
{
}
Camera.Parameters params = mCamera.getParameters();
String value;
if (isOn) // we are being ask to turn it on
{
value = Camera.Parameters.FLASH_MODE_TORCH;
}
else // we are being asked to turn it off
{
value = Camera.Parameters.FLASH_MODE_AUTO;
}
try{
params.setFlashMode(value);
mCamera.setParameters(params);
String nowMode = mCamera.getParameters().getFlashMode();
if (isOn)
{
nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH);
}
if (!isOn)
{
nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO);
}
}
catch (Exception ex)
{
}
}
}
but i got following exception
03-10 18:47:34.907: ERROR/QualcommCameraHardware(1280): Parameter AntiBanding is not supported for this sensor
You can do so by using Camera.Parameters.setFlashMode. See: http://developer.android.com/reference/android/hardware/Camera.Parameters.html
it is possible. first you have to get the properties of hardware camera.using below method.
String flattenString;
camera = Camera.open();
Camera.Parameters param = camera.getParameters();
flattenString = param.flatten();
where flatten can give u a parameter string with key value pair.
you have to split it by "," and find the flash-mode key and its value
then apply this parameter in below method
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("Key", value);
where your key is "flash-mode"
and value is "On"
and then add this parameters to setParameters method and start your camera preview
mCamera.setParameters(parameters);
mCamera.startPreview();
your flash light is now ON.

Categories

Resources