Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ArCameraServiceDemo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
maiqh
ArCameraServiceDemo
Commits
f6f2d0a4
Commit
f6f2d0a4
authored
Aug 13, 2020
by
maiqh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改不能重复打开camera的bug
parent
1038e1ed
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
19 deletions
+39
-19
GDArCameraService.java
...avi/amapauto/gdarcameraservicedemo/GDArCameraService.java
+39
-19
No files found.
CameraServiceDemo/app/src/main/java/com/autonavi/amapauto/gdarcameraservicedemo/GDArCameraService.java
View file @
f6f2d0a4
...
...
@@ -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,19 +104,29 @@ public class GDArCameraService extends Service {
public
boolean
initCamera
(
String
clientId
,
GDArCameraParam
gdArCameraParam
,
Surface
surface
)
throws
RemoteException
{
mCamera
=
null
;
mSurface
=
surface
;
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
;
}
mISCameraConnect
=
true
;
return
true
;
}
@Override
public
boolean
openCamera
(
String
clientId
)
throws
RemoteException
{
if
(
mCamera
==
null
)
{
mIsCameraOpened
=
false
;
return
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
);
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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment