在代码中设置摄像头参数的位置?

时间:2013-05-22 14:05:06

标签: android android-camera

我正在使用它来使用Android摄像头:

public class Login extends Activity实现了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) {}

}

它工作正常,但是当我进行相机交换时,应用会冻结。我第一次打开相机时设置了flashmode,它可以工作,但是当我进行交换时,我得到一个设置参数错误。我必须在哪里设置这个参数?

2 个答案:

答案 0 :(得分:1)

前置摄像头不支持闪光模式,也许下面的代码可以正常工作

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

答案 1 :(得分:0)

根据您的问题,请参阅Android Developers: Camera

基本上,您需要在surfacedChanged方法

中设置相机参数

在这里,为了您的信息,我复制/粘贴相关代码:

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());
        }
    }

如果有效,请告诉我