2013-05-22 31 views
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) {} 

}

它工作正常,但是當我做相機交換,該應用程序凍結。當我第一次打開相機時,我正在設置閃光模式,並且它可以工作,但是當我進行交換時,出現設置參數錯誤。我必須在哪裏設置這個參數?

+0

可我知道你原來的代碼你說'設置參數error',所以有什麼錯誤?你可以發佈一些日誌嗎? – dumbfingers

回答

1

的前置攝像頭不坐席端口閃光模式,也許如下面的代碼可以工作

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

是的......現在正在工作。謝謝! –

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

請讓我知道,如果它

+0

它不返回錯誤,但閃光燈不起作用。 –