Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera (Flashlight) API too slow

I making a Flashlight app, and I use Fragments. When I press the button, The lantern light delayed more than 4 seconds, and i don't know what happen. Also, when I Press the switch button another time, the Flashlight doesn't turn off Any idea?

Also I would like to make a stroboscopic lantern light with another button.

I search in internet but i dont find another option to make this feature, only this.

this is my code

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;

public class HerramientasFragment extends Fragment {

  private Camera cam;
  private Switch linterna;

  public HerramientasFragment() {
      // Required empty public constructor
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
      cam = Camera.open();
      super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
      View masterView =  inflater.inflate(R.layout.fragment_herramientas, container, false);
      linterna = (Switch) masterView.findViewById(R.id.switch_linterna);
      linterna.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Switch liternaSwitch = (Switch) v;

            Parameters p;

            if (liternaSwitch.isChecked()) {
                p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
            } else {
                p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
            }

        }
    });

    return masterView;
}

}
like image 851
Bryan Guamba Avatar asked Oct 31 '22 22:10

Bryan Guamba


1 Answers

It is possible that the 4 second delay is hardware/operating system related and out of your control. That's not to say it can't be fixed, but I am unable to find anything related to it (Some Android experts might have a better idea here).

The light not turning off is probably because you need to add cam.release(); as mentioned in this answer.

As for a stroboscopic light, I found this tutorial. It seems to be almost exactly what you are looking for.

like image 184
NitrogenReaction Avatar answered Nov 11 '22 09:11

NitrogenReaction