Commit f6f2d0a4 by maiqh

修改不能重复打开camera的bug

parent 1038e1ed
......@@ -16,7 +16,6 @@ import com.autonavi.amapauto.gdarcameraservice.IGDCameraStateCallBack;
import com.autonavi.amapauto.gdarcameraservice.IGDSize;
import com.autonavi.amapauto.gdarcameraservice.model.ArCameraOpenResultParam;
import com.autonavi.amapauto.gdarcameraservice.model.GDArCameraParam;
import com.autonavi.amapauto.gdarcameraservicedemo.utils.LogUtils;
import com.autonavi.amapauto.gdarcameraservicedemo.utils.SharedMemUtils;
import java.io.FileDescriptor;
......@@ -56,11 +55,6 @@ public class GDArCameraService extends Service {
@Override
public IBinder onBind(Intent intent) {
try {
mMemoryFile = new MemoryFile(MEMORY_NAME, MEMORY_SIZE);
} catch (IOException e) {
e.printStackTrace();
}
return binder;
}
......@@ -110,18 +104,28 @@ public class GDArCameraService extends Service {
public boolean initCamera(String clientId, GDArCameraParam gdArCameraParam, Surface surface) throws RemoteException {
mCamera = null;
mSurface = surface;
try {
mCamera = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
return false;
}
mISCameraConnect = true;
return true;
}
@Override
public boolean openCamera(String clientId) throws RemoteException {
if (mMemoryFile == null) {
try {
mMemoryFile = new MemoryFile(MEMORY_NAME, MEMORY_SIZE);
} catch (IOException e) {
e.printStackTrace();
}
}
if (mCamera == null) {
try {
mCamera = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
return false;
}
}
if (mCamera == null) {
mIsCameraOpened = false;
......@@ -155,17 +159,24 @@ public class GDArCameraService extends Service {
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (!mISCameraConnect) {
// 防止关闭摄像头后,还有回调,导致下一次打开屏幕发绿
return;
}
try {
// 这里有个异步导致下一次打开屏幕发绿的问题,目前简单通过mISCameraConnect暂时解决,到时正式接入时要考虑这个问题
sendData(data, camera);
} catch (IOException e) {
if (!mIsCameraOpened && mISCameraConnect) {
initMemoryFile();
mIsCameraOpened = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
mCamera.startPreview();
mPreviewSize = mCamera.getParameters().getPreviewSize();//获取尺寸,格式转换的时候要用到
initMemoryFile();
mIsCameraOpened = true;
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Error starting camera preview: " + e.getMessage());
......@@ -177,12 +188,19 @@ public class GDArCameraService extends Service {
@Override
public boolean closeCamera(String clientId) throws RemoteException {
if (mMemoryFile != null) {
mMemoryFile.close();
mMemoryFile = null;
}
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
mIsCameraOpened = false;
mISCameraConnect = false;
isFirstSend = true;
return true;
}
......@@ -190,7 +208,7 @@ public class GDArCameraService extends Service {
public boolean unInitCamera(String clientId) throws RemoteException {
mISCameraConnect = false;
mIsCameraOpened = false;
mCamera.release();
return true;
}
......@@ -198,7 +216,7 @@ public class GDArCameraService extends Service {
private byte[] header = new byte[SharedMemUtils.HEADER_SIZE];
private void sendData(byte[] bytes, Camera camera) throws IOException {
private synchronized void sendData(byte[] bytes, Camera camera) throws IOException {
byte[] buffer = new byte[HEADER_SIZE + bytes.length];
......@@ -214,7 +232,7 @@ public class GDArCameraService extends Service {
}
/** 判断共享内存是否可写 */
if (canWrite) {
if (canWrite && mISCameraConnect) {
SharedMemUtils.setOffset(buffer, 0);
SharedMemUtils.setLength(buffer, 0);
SharedMemUtils.setContentSize(buffer, bytes.length);
......@@ -222,7 +240,9 @@ public class GDArCameraService extends Service {
try {
SharedMemUtils.setCanRead(buffer);
mMemoryFile.writeBytes(buffer, 0, 0, buffer.length);
if (mISCameraConnect) {
mMemoryFile.writeBytes(buffer, 0, 0, buffer.length);
}
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "sendDataError: " + e.getMessage());
......@@ -231,7 +251,7 @@ public class GDArCameraService extends Service {
}
}
private void initMemoryFile() throws Exception {
private synchronized void initMemoryFile() throws Exception {
Method getFileDescriptorMethod = mMemoryFile.getClass().getDeclaredMethod("getFileDescriptor");
if (getFileDescriptorMethod != null) {
FileDescriptor fileDescriptor = (FileDescriptor) getFileDescriptorMethod.invoke(mMemoryFile);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment