i have made a flashlight app and i want to try to make add-on to allow a strobe light feature.
I want to set it up on a different button thought, not the same one. i think i need to use a timer , but i have never used a timer because im new to java.
here is my code for the flashlight:
public class FlashLightActivity extends Activity {
private boolean isLighOn = false;
private Camera camera;
private Button button;
#Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
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() {
public void onClick(View arg0) {
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
});
}
}
You could use a Handler
public class Strobe extends Activity {
private LinearLayout mLinearLayout;
private Handler mHander = new Handler();
private boolean mActive = false;
private boolean mSwap = true;
private final Runnable mRunnable = new Runnable() {
public void run() {
if (mActive) {
if (mSwap) {
mLinearLayout.setBackgroundColor(Color.WHITE);
mSwap = false;
mHander.postDelayed(mRunnable, 20);
} else {
mLinearLayout.setBackgroundColor(Color.BLACK);
mSwap = true;
mHander.postDelayed(mRunnable, 100);
}
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLinearLayout = (LinearLayout) findViewById(R.id.strobe);
startStrobe();
}
private void startStrobe() {
mActive = true;
mHander.post(mRunnable);
}
}
Set a Theme to the Activity to make it full screen.
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
Related
i am implementing flashlight, and i have called all the functions i,e onDestroy() onBackpressed() to remain my flash light on. but i have bug that if i pressed on back and after that screen is locked the flashlight switches off after few seconds. kindly help me with the code.
public class MainActivity extends AppCompatActivity {
ImageButton btnSwitch;
Camera camera;
Camera.Parameters parameters;
boolean isflash = false;
boolean ison = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH));
{
try {
camera = Camera.open();
parameters = camera.getParameters();
isflash = true;
}catch (Exception e ){
Log.e(getString(R.string.app_name), "failed to open Camera");
e.printStackTrace();
}
}
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isflash)
{
if (!ison)
{
btnSwitch.setImageResource(R.drawable.off);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
ison = true;
}
else {
btnSwitch.setImageResource(R.drawable.btn_switch_on);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
ison = false;
}
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Error.....");
builder.setMessage("FlashLight is not available on this device....");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
});
}
#Override
protected void onStop() {
super.onStop();
if(!ison){
if (camera!=null)
{
camera.release();
camera = null;
}
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(!ison){
if (camera!=null)
{
camera.release();
camera = null;
}
}
}
#Override
protected void onPause() {
super.onPause();
if(!ison){
if (camera!=null)
{
camera.release();
camera = null;
}
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(!ison) {
if (camera != null) {
camera.release();
camera = null;
}
}
}
}
public class CameraActivity
extends Activity
implements SurfaceHolder.Callback {
private LayoutInflater myInflater = null;
Camera myCamera;
byte[] tempdata;
boolean myPreviewRunning = false;
private SurfaceHolder mySurfaceHolder;
private SurfaceView mySurfaceView;
Button takePicture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera);
mySurfaceView = (SurfaceView) findViewById(R.id.surface);
if (mySurfaceHolder == null) {
mySurfaceHolder = mySurfaceView.getHolder();
}
mySurfaceHolder.addCallback(this);
mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myInflater = LayoutInflater.from(this);
View overView = myInflater.inflate(R.layout.second_layer_camera,null);
this.addContentView(overView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
takePicture = (Button) findViewById(R.id.button);
takePicture.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myCamera.takePicture(myShutterCallback, myPictureCallback, myJpeg);
}
});
}
ShutterCallback myShutterCallback = new ShutterCallback() {
#Override
public void onShutter() {
}
};
PictureCallback myPictureCallback = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera myCamera) {
}
};
PictureCallback myJpeg = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera myCamera) {
if(data != null){
tempdata = data;
done();
}
}
};
void done(){
Bitmap bm = BitmapFactory.decodeByteArray(tempdata, 0, tempdata.length);
String url = Images.Media.insertImage(getContentResolver(), bm, null, null);
bm.recycle();
Bundle bundle = new Bundle();
if(url != null){
bundle.putString("url",url);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
}
else{
Toast.makeText(this, "Picture can not be saved", Toast.LENGTH_SHORT).show();
}
// finish();
myCamera.startPreview();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try{
if(myPreviewRunning){
myCamera.stopPreview();
myPreviewRunning = false;
}
Camera.Parameters p = myCamera.getParameters();
p.setPreviewSize(width,height);
myCamera.setParameters(p);
myCamera.setPreviewDisplay(holder);
myCamera.startPreview();
myPreviewRunning = true;
}catch(Exception e){}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
myCamera = Camera.open();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
myCamera.stopPreview();
myPreviewRunning = false;
myCamera.release();
myCamera = null;
}
}
When launching the app, the custom camera appears rotated. I need to show a normal camera. I find the reason for it opens the wrong camera.
I did not find another way to create a customized camera.
You need to tell the OS the orientation of the screen in relationship to the orientation the camera's sensor is mounted in. The orientation of the screen can be rotated, the orientation of the sensor is fixed. You can use the reference code for setDisplayOrientation(int) which can be found here: http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29
I have a simple application with a button to turn on/off camera flash:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.btnFlash);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(!isLight)
{
switchON();
}
else
{
switchOFF();
}
}
});
}
If the flash was off, it will turn on and else, turn off. Yes, it works well.
The problem is:
- Firstly, I pressed the button to turn on, after that, I rotate my device and then pressing again to turn off -> Application crash.
Fatal Exception: main - Runtime Exception: Fail to connect to camera
service
These are 2 functions to turn on/off
public void switchON()
{
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLight = true;
}
public void switchOFF()
{
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
camera.release();
isLight = false;
}
Try This (I re-wrote the code):
public class YourClass extends Activity {
private Button button;
private Camera camera;
private boolean isLight=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.btnFlash);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(!isLight)
{
switchON();
}
else
{
switchOFF();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
try{
camera = Camera.open();
} catch( Exception e ){
e.printStackTrace();
}
}
#Override
protected void onPause() {
if( camera != null ){
camera.release();
camera = null;
}
super.onPause();
}
private void switchOFF(){
if( mCamera != null ){
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isLight = false;
}
}
private void switchON(){
if( mCamera != null ){
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isLight=true;
}
}
}
Try:
public void switchOFF()
{
if(camera == null)
{
camera = Camera.open();
}
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
camera.release();
isLight = false;
}
I have a Flashlight Activity. Normally it works fine but When I go to any other activity, it stop working!
So I want to refresh the code when I back to the Flashlight Activity.
I think refreshing using onResume() would help me best, But how to do it?
public class FlashLightActivity extends Activity {
//flag to detect flash is on or off
private boolean isLighOn = false;
private Camera camera;
private Button next1, next2;
#Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next1 = (Button) findViewById(R.id.ebtn28_answer);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), FullScreen.class);
startActivityForResult(myIntent, 0);
}
});
next2 = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
next2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
});
}
}
You need to override onPause and onResume. In onPause, you need to release the Camera. In onResume, you need to re-request it. Camera doesn't like it if you try to hold it when you aren't the active activity.
public void onPause(){
super.onPause();
if(camera != null){
camera.release();
camera = null;
}
}
public void onResume(){
super.onResume();
//Need to release if we already have one, or we won't get the camera
if(camera != null){
camera.release();
camera = null;
}
try {
camera = Camera.open();
}
catch (Exception e){
}
}
I am working with camera flashlight project.
It load on the emulator but when I click on the "On" button it has stopped unexpectedly. Here is the cat-log https://dl.dropbox.com/u/15065300/logcat1.png
Here is the code:
public class FlashLight extends Activity {
private final static String LOG_TAG = "FlashLight";
private Button mOnBtn;
private Camera mCamera;
private boolean isActive;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//this will be inside your onCreate...
mOnBtn = (Button) findViewById(R.id.on_btn);
mOnBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
flipSwitch();
processClick();
}
});
}
//these will be outside your onCreate
public void flipSwitch() {
isActive = !isActive;
}
#Override
protected void onResume() {
super.onResume();
try{
mCamera = Camera.open();
mCamera.startPreview();
Toast.makeText(getApplicationContext(),"Camera is present", Toast.LENGTH_LONG).show();
} catch( Exception e ){
Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
}
}
#Override
protected void onPause() {
if( mCamera != null ){
mCamera.release();
mCamera = null;
}
super.onPause();
}
public void processClick() {
if(isActive) {
Parameters params = mCamera.getParameters();
params.set("flash-mode", "torch");
mCamera.setParameters( params );
mCamera.startPreview();
}
else {
Parameters params = mCamera.getParameters();
params.set("flash-mode", "off");
mCamera.setParameters( params );
mCamera.stopPreview();
}
}
}
You have a NullPointerException on line 74 of Flashlight.java in your code.
It's probably that mCamera or params is being used while it's value is null. Check the initializations/assignments to these values.