Jairo Filho
Jairo Filho

Reputation: 342

Where in the code set camera parameters?

I'm using this to use android camera:

public class Login extends Activity implements SurfaceHolder.Callback {

public int idCamera(int id) {
    if (id == 0) { id = 1; } else { id = 0; }
    int tcam = Camera.getNumberOfCameras();
    if (tcam == 1) { id = 0; }
    return id;
}

public class idCameraV {
    public int id;
}

public static class camHolder {
    public static SurfaceHolder id;
}

private Camera camera;
private SurfaceView surfaceView;

static String senha2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    final idCameraV idCam = new idCameraV();
    idCam.id = 0;

    camera = Camera.open(idCam.id);
    Camera.Parameters parametro = camera.getParameters(); // WORKS OK
    parametro.setFlashMode("on"); // WORKS OK
    camera.setParameters(parametro); // WORKS OK

    surfaceView = (SurfaceView) findViewById(R.id.preview);
    surfaceView.getHolder().addCallback(this);

    final ImageButton button1 = (ImageButton) findViewById(R.id.bt_camera);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            camera.stopPreview();
            camera.release();
            idCam.id = idCamera(idCam.id);
            camera = Camera.open(idCam.id); 
            Camera.Parameters parametro = camera.getParameters();
            parametro.setFlashMode("on"); // THIS LINE AND ABOVE WORKS. I CAN READ BY GETFLASHMODE
            camera.setParameters(parametro); // ERROR IN HERE
            camera.startPreview();
                try {
            camera.setPreviewDisplay(camHolder.id);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    final ImageButton button2 = (ImageButton) findViewById(R.id.bt_login);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {                
            EditText senha = (EditText)findViewById(R.id.senha);
            senha2 = senha.getText().toString();                
            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            RelativeLayout aviso = (RelativeLayout) findViewById(R.id.aguarde);
            aviso.setVisibility(View.VISIBLE);              
            LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            String coordenadas = GPS.coordenadas(locationManager);
            String android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
            camera.takePicture(null, null, new TiraFoto(getApplicationContext(), android_id, coordenadas, aviso, connMgr, "LOGIN_", camera));
        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (camera != null) { camera.release(); }
}

@Override
protected void onPause() {
    super.onPause();
    if (camera != null) { camera.stopPreview(); }
}

@SuppressWarnings("static-access")
@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
        final camHolder camHolderId = new camHolder();
        camHolderId.id = holder;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    if (holder.getSurface() != null) {
        try {
            camera.stopPreview();
        } catch (Exception e) {
    }
        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {}

}

It works ok, but when I do the camera swap, the app freezes. I'm setting the flashmode when I open the camera first time, and it works, but when I do the swap, I get a set parameter error. Where I must set this parameters?

Upvotes: 2

Views: 4611

Answers (2)

seeside
seeside

Reputation: 26

The front camera doesn't support flash mode, maybe the code as below can work

if(idCam.id == 0)
    parametro.setFlashMode("on");

Upvotes: 1

dumbfingers
dumbfingers

Reputation: 7089

According to your question, please refer to the Android Developers: Camera

Basically, you'll need to set the camera parameters inside surfacedChanged method

And here for your information, I copy/paste the related code:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

Please let me know if it works

Upvotes: 0

Related Questions