首先,鄙人要發泄下對Android小小的不滿,為神馬2.3才有api來支援front Camera ,為神馬2.3之前的版本也會有自拍,為神馬每個廠商的調用自拍的方式都不一樣。尼瑪同樣是寫程式,至於搞那麼多不同版本不同相容性問題麼。生物識別技術技術你們傷不起啊!!!GOOGLE 您神馬時候才能統一啊!!!別折磨我們這群蛋疼的碼農了。該死的魂淡廠商們,你們大致統一一下行不行,坑爹的。
Creates a new Camera object to access a particular hardware camera.
cameraId the hardware camera to access, between 0 and
getNumberOfCameras()
-1.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {//這就是自拍,親。
c = Camera.open(i);
}
}
}
if (c == null) {
c = Camera.open();
}
c.unlock();//注意,要在MediaRecorder設定參數之前就調用unlock來獲得camera的控制權。camera是單例的嘛。如果不調用,程式就掛
recorder.setCamera(c);
void android.hardware.Camera.unlock()
Unlocks the camera to allow another process to access it. Normally, the camera is locked to the process with an active Camera object until release()
is called. To allow rapid handoff between processes, you can call this method to release the camera temporarily for another process to use; once the other process is done you can call reconnect()
to reclaim the camera.
This must be done before calling android.media.MediaRecorder.setCamera(Camera)
.
If you are not recording video, you probably do not need this method.
這樣就大功告成了。最後還有要注意的地方,就是當你停止拍攝或程式down掉的時候,要調用camera.lock()來釋放camera的控制權,否則就不能再開啟camera。知道拋異常。
private void stop(boolean force) {
if (!force) {
recorder.stop();
}
recorder.reset();
recorder.release();
recorder = null;
if (c != null) {
c.lock();
c.release();
c = null;
}
setResult(force ? Activity.RESULT_CANCELED : Activity.RESULT_OK);
//還記得,調用系統的camera來拍照時,intent裡會返迴文件的uri。你也可以這樣做 intent.setData(Uri.fromFile(tempFile))
this.finish();
}
下面再來說2.3之前的如何調用前置的。公司只有HTC野火和SUMSUNG I9000有前置,所以我只研究了這兩個,有其他brand的可以用類似的方法去做,如果你弄出來了,請告訴我,mark下
debug還是比較有趣的事情,我是指能成功的debug的話。。。。。
測試這兩個手機的時候我一行一行的看log。眼睛都花了,一遍咒罵宏達電和三星。
06-23 17:49:05.058: WARN/CameraService(2374): getParameters(AppShutterSound=0;anti-shake=0;antibanding=auto;antibanding-values=auto,50hz,60hz,off;beauty-shot=0;blur=0;camera-id=1;chk_dataline=0;contrast=2;contrast-max=4;contrast-min=0;effect=none;effect-values=none,mono,negative,sepia;exposure-compensation=0;exposure-compensation-step=0.5;focal-length=3.79;focus-mode=auto;focus-mode-values=auto,macro;horizontal-view-angle=51.2;iso=auto;jpeg-quality=100;jpeg-thumbnail-height=120;jpeg-thumbnail-quality=100;jpeg-thumbnail-size-values=160x120,0x0;jpeg-thumbnail-width=160;max-exposure-compensation=4;max-zoom=30;metering=center;min-exposure-compensation=-4;picture-format=jpeg;picture-format-values=jpeg;picture-size=2560x1920;picture-size-values=2560x1920,2048x1536,1600x1200,640x480,2560x1536,2048x1232,1600x960,800x480;preview-format=yuv420sp;preview-format-values=yuv420sp;preview-frame-rate=30;preview-frame-rate-values=15,30;preview-size=640x480;preview-size-values=320x240,640x480,800x480;rotation=0;saturation=2;saturation-max=4;saturation-min=0;scene-mode=au
這是三星的Camera.Parameters 你覺得哪個是設定自拍的麼 camera-id=1 bingo 就是它了。
// Parameters parameters = c.getParameters();
// parameters.set("camera-id",2);//sumsung 2.3以前的手機 我的I9000成功設定成前置的了。
06-23 18:14:50.697: DEBUG/CameraService(1622): getParameters(Make=HTC;antibanding=auto;antibanding-values=auto,50hz,60hz;brightness=0;brightness-def=0;brightness-max=20;brightness-min=-20;contrast=0;contrast-def=0;contrast-max=100;contrast-min=-100;device=;device-list=/dev/video0,/dev/video1,/dev/video2;effect=none;effect-values=none,mono,negative,sepia,aqua;exposure-compensation=0;exposure-compensation-step=0;fnumber=3.5;focal-length=2.76;focus-mode=auto;focus-mode-values=auto;gps-mapdatum=WGS 84;horizontal-view-angle=54;iso=auto;jpeg-quality=85;jpeg-thumbnail-height=240;jpeg-thumbnail-size-values=320x240,0x0;jpeg-thumbnail-width=320;max-exposure-compensation=0;max-zoom=10;meter-mode=meter-average;min-exposure-compensation=0;models=A3360;picture-format=jpeg;picture-format-values=jpeg;picture-size=2048x1536;picture-size-values=2048x1536,1600x1200,1280x1024,1280x960,1024x768,800x600,640x480,384x288,352x288,320x240,176x144;preview-format=yuv420sp;preview-format-values=yuv420sp;preview-frame-rate=15;preview-frame-rate-values=30,15;preview-size=320x240;previ
好了,這是宏達電的,你猜哪個是? 不好意思,木有這個參數。當時我就鬱悶了。Android程式員你們都傷不起啊。一堆的相容性問題。尼瑪你們來試試。
腫麼辦,好吧,去看系統內建的Camera吧,如果你懂得觀察,還是比較有意思的。野火的後置網路攝影機叫 (主) 你們有這個機器的可以自己測試下。
06-23 18:14:50.716: DEBUG/CameraService(1622): setParameters: video_input= main(有意思吧)
debug吧,坑爹的叫video_input 尼瑪弄不一樣的key有意思麼,搞這麼混亂讓人腫麼活。人家做愛瘋的,從底層自己寫上來,一遍就ok啦。有木有。我們有一大堆的api,半小時就能寫出來的功能,debug就要幾天。有木有!!!
你再試試吧系統的Camera切換成副網路攝影機叫什麼。。。Secondary。有木有!!!
好吧。video_input= Secondary 去試吧。依舊不行。。。S小寫。尼瑪傷不起啊
// Parameters parameters = c.getParameters();
// parameters.set("camera-id",2);//sumsung 2.3以前的手機 前置
// parameters.set("camera-id",1);//sumsung 2.3以前的手機 後置
// parameters.set("video_input","secondary");//htc2.3以前的手機 前置
// parameters.set("video_input","main");//htc2.3以前的手機 後置
// c.setParameters(parameters);
ok,都這裡就全部結束了。蛋疼的debug。蛋疼的Android。蛋疼的人生。碼農你們傷不起啊!!!!