|
package com.bairuitech.anychat;
import java.util.List;
import com.soufun.agent.utils.UtilsLog;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
// AnyChat Camera��װ�࣬ʵ�ֱ�����Ƶ�ɼ�
public class AnyChatCameraHelper implements SurfaceHolder.Callback{
private final static String TAG = "ANYCHAT";
private Camera mCamera =null;
private boolean bIfPreview = false;
private boolean bNeedCapture = false;
private int iCurrentCameraId = 0;
private SurfaceHolder currentHolder = null;
public final int CAMERA_FACING_BACK = 0;
public final int CAMERA_FACING_FRONT = 1;
// ��ʼ���������surfaceCreated�е���
private void initCamera()
{
Log.e("zhaoxiangji", "=====31====line====");
if (null == mCamera)
return;
try {
if (bIfPreview) {
mCamera.stopPreview();// stopCamera();
mCamera.setPreviewCallback(null);
Log.e("zhaoxiangji", "=====38==line=====");
}
/* Camera Service settings */
Camera.Parameters parameters = mCamera.getParameters();
// ��ȡcamera֧�ֵ���ز����ж��Ƿ��������
List<Size> previewSizes = mCamera.getParameters().getSupportedPreviewSizes();
// ��ȡ��ǰ���õķֱ��ʲ���
int iSettingsWidth = AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL);
int iSettingsHeight = AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL);
boolean bSetPreviewSize = false;
for (int i = 0; i < previewSizes.size(); i++) {
Size s = previewSizes.get(i);
Log.e("zhaoxiangji", "=====52==line=====");
if(s.width == iSettingsWidth && s.height == iSettingsHeight) {
bSetPreviewSize = true;
parameters.setPreviewSize(iSettingsWidth, iSettingsHeight);
Log.e("zhaoxiangji", "=====56==line=====");
break;
}
}
parameters.setPreviewFrameRate(25);
// ָ���ķֱ��ʲ�֧��ʱ����Ĭ�ϵķֱ������
if(!bSetPreviewSize)
parameters.setPreviewSize(320, 240);
// ������Ƶ��ݸ�ʽ
parameters.setPreviewFormat(ImageFormat.NV21);
// ����������Ч
try {
mCamera.setParameters(parameters);
Log.e("zhaoxiangji", "=====70==line=====");
} catch(Exception e){
}
// ������Ƶ����ص�����ͨ��AnyChat���ⲿ��Ƶ����ӿڴ���AnyChat�ں˽��д���
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if(data.length !=0 && bNeedCapture)
AnyChatCoreSDK.InputVideoData(data, data.length, 0);
Log.e("zhaoxiangji", "=====80==line=====");
}
});
mCamera.startPreview(); // ��Ԥ������
bIfPreview = true;
// ��ȡ���ú����ز���
int pixfmt = -1;
if(mCamera.getParameters().getPreviewFormat() == ImageFormat.NV21)
pixfmt = AnyChatDefine.BRAC_PIX_FMT_NV21;
else if(mCamera.getParameters().getPreviewFormat() == ImageFormat.YV12)
pixfmt = AnyChatDefine.BRAC_PIX_FMT_YV12;
else if(mCamera.getParameters().getPreviewFormat() == ImageFormat.NV16)
pixfmt = AnyChatDefine.BRAC_PIX_FMT_NV16;
else if(mCamera.getParameters().getPreviewFormat() == ImageFormat.YUY2)
pixfmt = AnyChatDefine.BRAC_PIX_FMT_YUY2;
else if(mCamera.getParameters().getPreviewFormat() == ImageFormat.RGB_565)
pixfmt = AnyChatDefine.BRAC_PIX_FMT_RGB565;
else
Log.e(TAG, "unknow camera privew format:" + mCamera.getParameters().getPreviewFormat());
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_EXTVIDEOINPUT, 1);
AnyChatCoreSDK.SetInputVideoFormat(pixfmt, previewSize.width, previewSize.height, mCamera.getParameters().getPreviewFrameRate(), 0);
} catch (Exception e) {
Log.e("zhaoxiangji", "=====105==line=====");
e.printStackTrace();
}
}
// ����ͷ�ɼ�����
public void CaptureControl(boolean bCapture) {
bNeedCapture = bCapture;
}
// ��ȡϵͳ������ͷ������
public int GetCameraNumber() {
try {
return Camera.getNumberOfCameras();
} catch (Exception ex) {
ex.printStackTrace();
return 0;
}
}
// �Զ��Խ�
public void CameraAutoFocus() {
if(mCamera == null || !bIfPreview)
return;
try {
mCamera.autoFocus(null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// �л�����ͷ
public void SwitchCamera() {
try {
if(Camera.getNumberOfCameras() == 1 || currentHolder == null)
return;
iCurrentCameraId = (iCurrentCameraId==0) ? 1 : 0;
if(null != mCamera) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
bIfPreview = false;
mCamera.release();
mCamera = null;
}
mCamera = Camera.open(iCurrentCameraId);
mCamera.setPreviewDisplay(currentHolder);
initCamera();
} catch (Exception ex) {
ex.printStackTrace();
if(null != mCamera) {
mCamera.release();
mCamera = null;
}
}
}
// ѡ������ͷ
public void SelectVideoCapture(int facing) {
for (int i = 0; i < Camera.getNumberOfCameras(); i++)
{
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == facing) {
iCurrentCameraId = i;
break;
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.e("zhaoxiangji", "=====176==line=====");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
Log.e("zhaoxiangji", "=====182==line=====");
mCamera = Camera.open(iCurrentCameraId);
currentHolder = holder;
mCamera.setPreviewDisplay(holder);//set the surface to be used for live preview
initCamera();
} catch (Exception ex) {
Log.e("zhaoxiangji", "=====188==line=====");
ex.printStackTrace();
if(null != mCamera) {
mCamera.release();
mCamera = null;
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(null != mCamera) {
try {
Log.e("zhaoxiangji", "=====201==line=====");
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
bIfPreview = false;
mCamera.release();
mCamera = null;
} catch (Exception ex) {
Log.e("zhaoxiangji", "=====208==line=====");
mCamera = null;
bIfPreview = false;
}
}
currentHolder = null;
}
}
该打的地方都打印了。AnyChatCameraHelper 在日志都一个没打印出来。其他的日志都是可以打印的。
正常情况下日志:
07-29 15:00:56.729: E/zhaoxiangji(9864): =====182==line=====
07-29 15:00:58.649: E/zhaoxiangji(9864): =====31====line====
07-29 15:00:58.789: E/zhaoxiangji(9864): =====52==line=====
07-29 15:00:58.789: E/zhaoxiangji(9864): =====56==line=====
07-29 15:00:58.809: E/zhaoxiangji(9864): =====70==line=====
07-29 15:00:59.429: E/zhaoxiangji(9864): =====176==line=====
不正常情况下,一个也打不出来。
麻烦提供下思路。 |
|