2

我正在使用它来使用android相机:

公共类登录扩展活动实现 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) {}

}

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

4

2 回答 2

1

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

if(idCam.id == 0)
    parametro.setFlashMode("on");
于 2013-05-22T15:55:59.207 回答
0

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

于 2013-05-22T14:21:42.547 回答