一、相機(jī)相關(guān)參數(shù)
1.相機(jī)id
根據(jù)指定的相機(jī)id打開相機(jī)。
//Camera.CameraInfo.CAMERA_FACING_BACK,Camera.CameraInfo.CAMERA_FACING_FRONT
int mCameraId = Camera.CameraInfo.*CAMERA_FACING_FRONT*;
mCamera = Camera.*open*(mCameraId);
相機(jī)id對(duì)應(yīng)著Camera.CameraInfo的facing字段。
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int i = 0; i < Camera.*getNumberOfCameras*(); i++) {
Camera.*getCameraInfo*(i, cameraInfo);
Log.*d*(*TAG*, "getCameraInstance: camera facing=" + cameraInfo.facing + ",camera orientation=" + cameraInfo.orientation);
}
2.圖像格式
可以通過Camera.Parameters.getSupportedPreviewFormats()獲取支持的圖像格式列表,通過Camera.Parameters.setPreviewFormat(pixel_format)來設(shè)置圖像格式。
List<Integer> supportedPreviewFormats = parameters.getSupportedPreviewFormats();
parameters.setPreviewFormat(ImageFormat.NV21);
默認(rèn)格式為NV21,建議選擇的顏色格式為NV21和YV12,這兩個(gè)是所有機(jī)型均支持的。
NV21: YYYYYYYY VU VU => YUV420SP
YV12: YYYYYYYY VV UU => YUV420P
二者均為 YUV 4:2:0采樣,即每四個(gè)Y共用一組UV分量。其大小為 width * heigh * 3/2 byte。
3.大?。▽挾?、高度)
大小包括預(yù)覽尺寸(PreviewSize)和圖像尺寸(PictureSize),最好選擇一致。
可以通過Camera.Parameters.getSupportedPreviewSizes()獲取手機(jī)支持的Size列表,選擇所需要的大小。通過Camera.Parameters.setPreviewSize和setPictureSize設(shè)置大小。
List<Camera.Size> mapSizes = parameters.getSupportedPreviewSizes();
parameters.setPreviewSize(mImageSize.width, mImageSize.height);
parameters.setPictureSize(mImageSize.width, mImageSize.height);
一般是根據(jù)期望的范圍,選擇一個(gè)合適的大小。
private Camera.Size getPreferredPreviewSize(Camera.Parameters parameters, int width, int height) {
List<Camera.Size> mapSizes = parameters.getSupportedPreviewSizes();
List<Camera.Size> collectorSizes = new ArrayList<>();
for (Camera.Size option : mapSizes) {
Log.i(TAG, " option.width=" + option.width + " option.height=" + option.height);
if (width > height) {
if (option.width >= width && option.height >= height) {
collectorSizes.add(option);
}
} else {
if (option.width >= height && option.height >= width) {
collectorSizes.add(option);
}
}
}
if (collectorSizes.size() > 0) {
return Collections.min(collectorSizes, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size lhs, Camera.Size rhs) {
return Long.signum(lhs.width * lhs.height - rhs.width * rhs.height);
}
});
}
return mapSizes.get(0);
}
4.角度
相機(jī)錄制的圖像數(shù)據(jù),需要經(jīng)過旋轉(zhuǎn)甚至翻轉(zhuǎn)后才會(huì)正常顯示。
旋轉(zhuǎn)的角度,可以通過Camera.CameraInfo.orientation獲取。
此外,還要考慮兩個(gè)因素,一是屏幕的角度,可以通過WindowManager.getDefaultDisplay().getRotation()獲??;二是對(duì)于前攝,還要再水平翻轉(zhuǎn)下,不過,底層相機(jī)輸送到Surface中的數(shù)據(jù),已經(jīng)是水平翻轉(zhuǎn)過的。
所以,對(duì)于預(yù)覽顯示,只需要Camera.Parameters.setDisplayOrientation(degrees)設(shè)置需要旋轉(zhuǎn)的角度即可。
mCamera.setDisplayOrientation(degrees); //順時(shí)針旋轉(zhuǎn)角度
如何計(jì)算需要旋轉(zhuǎn)的角度呢?
public int getCameraDisplayOrientation(Activity activity, int cameraId) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0; //屏幕角度
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info); //info.orientation 在正常屏幕上需要旋轉(zhuǎn)的相機(jī)角度
int result; //最終需要的相機(jī)旋轉(zhuǎn)角度
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
// 前置攝像頭作鏡像翻轉(zhuǎn)
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
Log.i(TAG, " initCamera: rotationDegrees=" + result + " CameraOrientation=" + info.orientation + " DisplayDegrees=" + degrees);
return result;
}
5. 幀率
設(shè)置的幀率,只是一個(gè)目標(biāo)幀率值,具體的幀率取決于驅(qū)動(dòng)。
parameters.setPreviewFrameRate(20);
不過,Android不推薦set固定幀率,而是推薦設(shè)置一個(gè)幀率范圍。需要留意的是,調(diào)用setPreviewFpsRange設(shè)置的最大值和最小值,除以1000后才是幀率值。
parameters.setPreviewFpsRange(7000, 30000);
幀率范圍的最大值和最小值,不是自己隨便寫的,而是通過Camera.Parameters.getSupportedPreviewFpsRange() 來獲取。
二、視頻流采集
1.初始化好呈現(xiàn)視頻流的控件(如果不需要呈現(xiàn),就不必走該步驟)
SurfaceView、TextureView。推薦后者。
通過surfaceTextureListener來拿到回調(diào)結(jié)果。
TextureView.setSurfaceTextureListener(surfaceTextureListener);
SurfaceTextureListener surfaceTextureListener = new SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.i(TAG, " onSurfaceTextureAvailable width=" + width + " height=" + height);
VideoCapture.this.surface = surface;
mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
initCamera(surface, mCameraId);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.i(TAG, " onSurfaceTextureSizeChanged width=" + width + " height=" + height);
autoFocusCamera();
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.i(TAG, " onSurfaceTextureDestroyed");
destroyCamera();
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
Log.i(TAG, " onSurfaceTextureUpdated");
}
};
2.初始化相機(jī)
根據(jù)相機(jī)id打開相機(jī)。
設(shè)置預(yù)覽尺寸、圖像尺寸、預(yù)覽格式、自動(dòng)對(duì)焦等。
設(shè)置預(yù)覽Surface、旋轉(zhuǎn)角度、相機(jī)回調(diào)。
int width = Constant.DEFAULT_VIDEO_WIDTH, height = Constant.DEFAULT_VIDEO_HEIGHT; //1920*1080 640*360 720*480
mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
mCamera = Camera.open(mCameraId);
Camera.Parameters parameters = mCamera.getParameters();
mImageSize = getPreferredPreviewSize(parameters, width, height);
Log.i(TAG, " initCamera: surface width==" + mImageSize.width + ",height==" + mImageSize.height);
parameters.setPreviewFpsRange(7000, 30000);
parameters.setPreviewSize(mImageSize.width, mImageSize.height); //設(shè)置預(yù)覽尺寸onPreviewFrame的尺寸
parameters.setPictureSize(mImageSize.width, mImageSize.height); //設(shè)置拍照輸出圖片尺寸
parameters.setPreviewFormat(ImageFormat.NV21);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);//設(shè)置自動(dòng)對(duì)焦
mCamera.setParameters(parameters);
mCamera.setPreviewCallback(cameraPreviewCallback);
mCamera.setPreviewTexture(surface); //設(shè)置顯示圖像的TextureView
mCamera.setDisplayOrientation(getCameraDisplayOrientation((Activity) mContext, mCameraId)); //設(shè)置展示圖像的旋轉(zhuǎn)角度
3.開始掃描
調(diào)用startPreview開始掃描。
mCamera.startPreview();
4.在相機(jī)預(yù)覽回調(diào)中拿到數(shù)據(jù)
初始化相機(jī)的時(shí)候,已經(jīng)設(shè)置了相機(jī)預(yù)覽回調(diào)cameraPreviewCallback。
private Camera.PreviewCallback cameraPreviewCallback =new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// data數(shù)組即為視頻流數(shù)據(jù)
}
};
三、視頻流數(shù)據(jù)的處理
前文已經(jīng)提到,視頻流數(shù)據(jù)需要旋轉(zhuǎn)甚至翻轉(zhuǎn)才能夠正常顯示。通過調(diào)用setDisplayOrientation就可以直接輸送到Surface上顯示了。如果我們自己處理呢?
1.后攝,旋轉(zhuǎn)
以我的小米5s為例,豎屏放置。可以得出:
rotationDegrees=90 CameraOrientation=90 DisplayDegrees=0
即最終需要旋轉(zhuǎn)的角度為90度。
/**
* 順時(shí)針旋轉(zhuǎn)90度
* Y數(shù)據(jù):yuv[i] = data[y * imageWidth + x]
* UV數(shù)據(jù):
* 需求:后置鏡頭,數(shù)據(jù)需要順時(shí)針旋轉(zhuǎn)90度才顯示正常。
* 備注:這里的寬和高,都是原始影像中的寬和高。
*/
private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
// Rotate the Y luma
int i = 0;
for (int x = 0; x < imageWidth; x++) {
for (int y = imageHeight - 1; y >= 0; y--) {
yuv[i] = data[y * imageWidth + x];
i++;
}
}
// Rotate the U and V color components
i = imageWidth * imageHeight * 3 / 2 - 1;
for (int x = imageWidth - 1; x > 0; x = x - 2) {
for (int y = 0; y < imageHeight / 2; y++) {
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
i--;
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
i--;
}
}
return yuv;
}
2.前攝,旋轉(zhuǎn)+水平翻轉(zhuǎn)
在Surface上顯示時(shí),是旋轉(zhuǎn)底層系統(tǒng)已經(jīng)水平翻轉(zhuǎn)過的數(shù)據(jù)。旋轉(zhuǎn)角度即為前文計(jì)算的角度。
我們自己處理,是按照正規(guī)的順序,先旋轉(zhuǎn),再水平翻轉(zhuǎn)。這兩個(gè)旋轉(zhuǎn)角度是不一樣的,我的小米5s手機(jī),豎屏顯示,自己旋轉(zhuǎn)的角度是270度,即CameraOrientation給出的角度。
/**
* 順時(shí)針旋轉(zhuǎn)270度 + 水平鏡像翻轉(zhuǎn)
* Y數(shù)據(jù):
* UV數(shù)據(jù):
* 需求:前置鏡頭,數(shù)據(jù)需要順時(shí)針旋轉(zhuǎn)270度,再水平鏡像翻轉(zhuǎn)才顯示正常。
* 備注:這里的寬和高,都是原始影像中的寬和高。
*/
private byte[] rotateYUVDegree270AndMirror(byte[] data, int imageWidth, int imageHeight) {
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
// Rotate and mirror the Y luma
int i = 0;
int maxY = 0;
for (int x = imageWidth - 1; x >= 0; x--) {
maxY = imageWidth * (imageHeight - 1) + x * 2;
for (int y = 0; y < imageHeight; y++) {
yuv[i] = data[maxY - (y * imageWidth + x)];
i++;
}
}
// Rotate and mirror the U and V color components
int uvSize = imageWidth * imageHeight;
i = uvSize;
int maxUV = 0;
for (int x = imageWidth - 1; x > 0; x = x - 2) {
maxUV = imageWidth * (imageHeight / 2 - 1) + x * 2 + uvSize;
for (int y = 0; y < imageHeight / 2; y++) {
yuv[i] = data[maxUV - 2 - (y * imageWidth + x - 1)];
i++;
yuv[i] = data[maxUV - (y * imageWidth + x)];
i++;
}
}
return yuv;
}
3.格式轉(zhuǎn)換
預(yù)覽數(shù)據(jù)格式為NV21,如果用于編碼的話,編碼格式指定MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible,需要先將數(shù)據(jù)轉(zhuǎn)換為NV12。
轉(zhuǎn)換方法如下:
/**
* 將NV21格式數(shù)據(jù)轉(zhuǎn)換為NV12格式數(shù)據(jù)
* NV12與NV21類似,U 和 V 交錯(cuò)排列,不同在于UV順序。
* NV12: YYYYYYYY UVUV =>YUV420SP
* NV21: YYYYYYYY VUVU =>YUV420SP
*/
private void NV21ToNV12(byte[] nv21, byte[] nv12, int width, int height) {
if (nv21 == null || nv12 == null) return;
int framesize = width * height;
int i = 0, j = 0;
System.arraycopy(nv21, 0, nv12, 0, framesize);
for (i = 0; i < framesize; i++) {
nv12[i] = nv21[i];
}
for (j = 0; j < framesize / 2; j += 2) {
nv12[framesize + j - 1] = nv21[j + framesize];
}
for (j = 0; j < framesize / 2; j += 2) {
nv12[framesize + j] = nv21[j + framesize - 1];
}
}
四、圖解視頻流數(shù)據(jù)的旋轉(zhuǎn)與翻轉(zhuǎn)
前文講到了視頻流數(shù)據(jù)的旋轉(zhuǎn)與翻轉(zhuǎn),為便于理解,畫圖說明。
我用的手機(jī)為小米5s,將Activity寫死為豎屏。
先亮圖:

對(duì)于后攝,前文已經(jīng)提到:
rotationDegrees=90 CameraOrientation=90 DisplayDegrees=0
坐標(biāo)系1就是呈現(xiàn)給用戶的坐標(biāo),坐標(biāo)系2是Camera錄制視頻流的坐標(biāo)。
看圖就容易明白了,旋轉(zhuǎn)90度才是正常的顯示。不做旋轉(zhuǎn)的話,呈現(xiàn)出來的圖像就如坐標(biāo)系2所示,可以調(diào)試看看效果。
再看前攝,前文也提到:
rotationDegrees=270 CameraOrientation=90 DisplayDegrees=0
坐標(biāo)系1同樣是呈現(xiàn)給用戶的坐標(biāo),而坐標(biāo)系3是Camera錄制視頻流的坐標(biāo)。
看圖,容易明白,先將坐標(biāo)系2旋轉(zhuǎn)270度到坐標(biāo)系4,而后再水平翻轉(zhuǎn)到坐標(biāo)系1。
順便說一下,為什么前攝,setDisplayOrientation的角度也是90度,因?yàn)榈讓酉鄼C(jī)輸送到Surface中的數(shù)據(jù),已經(jīng)是水平翻轉(zhuǎn)過的,也就是已經(jīng)水平翻轉(zhuǎn)到坐標(biāo)系2了,解下來只需再旋轉(zhuǎn)90度即可。