Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
b37d65ff
Commit
b37d65ff
authored
Mar 12, 2013
by
Alexander Smorkalov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code refactoring was done.
parent
dc2bca07
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
45 deletions
+44
-45
native.cpp
samples/android/native-activity/jni/native.cpp
+44
-45
No files found.
samples/android/native-activity/jni/native.cpp
View file @
b37d65ff
...
@@ -28,14 +28,14 @@ struct Engine
...
@@ -28,14 +28,14 @@ struct Engine
cv
::
Ptr
<
cv
::
VideoCapture
>
capture
;
cv
::
Ptr
<
cv
::
VideoCapture
>
capture
;
};
};
cv
::
Size
calc
OptimalR
esolution
(
const
char
*
supported
,
int
width
,
int
height
)
cv
::
Size
calc
_optimal_camera_r
esolution
(
const
char
*
supported
,
int
width
,
int
height
)
{
{
int
frame
W
idth
=
0
;
int
frame
_w
idth
=
0
;
int
frame
H
eight
=
0
;
int
frame
_h
eight
=
0
;
size_t
prev_idx
=
0
;
size_t
prev_idx
=
0
;
size_t
idx
=
0
;
size_t
idx
=
0
;
float
min
D
iff
=
FLT_MAX
;
float
min
_d
iff
=
FLT_MAX
;
do
do
{
{
...
@@ -52,11 +52,11 @@ cv::Size calcOptimalResolution(const char* supported, int width, int height)
...
@@ -52,11 +52,11 @@ cv::Size calcOptimalResolution(const char* supported, int width, int height)
int
h_diff
=
height
-
tmp_height
;
int
h_diff
=
height
-
tmp_height
;
if
((
h_diff
>=
0
)
&&
(
w_diff
>=
0
))
if
((
h_diff
>=
0
)
&&
(
w_diff
>=
0
))
{
{
if
((
h_diff
<=
min
D
iff
)
&&
(
tmp_height
<=
720
))
if
((
h_diff
<=
min
_d
iff
)
&&
(
tmp_height
<=
720
))
{
{
frame
W
idth
=
tmp_width
;
frame
_w
idth
=
tmp_width
;
frame
H
eight
=
tmp_height
;
frame
_h
eight
=
tmp_height
;
min
D
iff
=
h_diff
;
min
_d
iff
=
h_diff
;
}
}
}
}
...
@@ -64,15 +64,13 @@ cv::Size calcOptimalResolution(const char* supported, int width, int height)
...
@@ -64,15 +64,13 @@ cv::Size calcOptimalResolution(const char* supported, int width, int height)
}
while
(
supported
[
idx
-
1
]
!=
'\0'
);
}
while
(
supported
[
idx
-
1
]
!=
'\0'
);
return
cv
::
Size
(
frame
Width
,
frameH
eight
);
return
cv
::
Size
(
frame
_width
,
frame_h
eight
);
}
}
static
void
engine_draw_frame
(
Engine
*
engine
,
const
cv
::
Mat
&
frame
)
static
void
engine_draw_frame
(
Engine
*
engine
,
const
cv
::
Mat
&
frame
)
{
{
if
(
engine
->
app
->
window
==
NULL
)
if
(
engine
->
app
->
window
==
NULL
)
{
return
;
// No window.
return
;
// No window.
}
ANativeWindow_Buffer
buffer
;
ANativeWindow_Buffer
buffer
;
if
(
ANativeWindow_lock
(
engine
->
app
->
window
,
&
buffer
,
NULL
)
<
0
)
if
(
ANativeWindow_lock
(
engine
->
app
->
window
,
&
buffer
,
NULL
)
<
0
)
...
@@ -83,10 +81,13 @@ static void engine_draw_frame(Engine* engine, const cv::Mat& frame)
...
@@ -83,10 +81,13 @@ static void engine_draw_frame(Engine* engine, const cv::Mat& frame)
void
*
pixels
=
buffer
.
bits
;
void
*
pixels
=
buffer
.
bits
;
for
(
int
yy
=
0
;
yy
<
std
::
min
(
frame
.
rows
,
buffer
.
height
);
yy
++
)
int
left_indent
=
(
buffer
.
width
-
frame
.
cols
)
/
2
;
int
top_indent
=
(
buffer
.
height
-
frame
.
rows
)
/
2
;
for
(
int
yy
=
top_indent
;
yy
<
std
::
min
(
frame
.
rows
+
top_indent
,
buffer
.
height
);
yy
++
)
{
{
unsigned
char
*
line
=
(
unsigned
char
*
)
pixels
;
unsigned
char
*
line
=
(
unsigned
char
*
)
pixels
;
memcpy
(
line
,
frame
.
ptr
<
unsigned
char
>
(
yy
),
memcpy
(
line
+
left_indent
*
4
*
sizeof
(
unsigned
char
)
,
frame
.
ptr
<
unsigned
char
>
(
yy
),
std
::
min
(
frame
.
cols
,
buffer
.
width
)
*
4
*
sizeof
(
unsigned
char
));
std
::
min
(
frame
.
cols
,
buffer
.
width
)
*
4
*
sizeof
(
unsigned
char
));
// go to next line
// go to next line
pixels
=
(
int32_t
*
)
pixels
+
buffer
.
stride
;
pixels
=
(
int32_t
*
)
pixels
+
buffer
.
stride
;
...
@@ -109,32 +110,36 @@ static void engine_handle_cmd(android_app* app, int32_t cmd)
...
@@ -109,32 +110,36 @@ static void engine_handle_cmd(android_app* app, int32_t cmd)
union
{
double
prop
;
const
char
*
name
;}
u
;
union
{
double
prop
;
const
char
*
name
;}
u
;
u
.
prop
=
engine
->
capture
->
get
(
CV_CAP_PROP_SUPPORTED_PREVIEW_SIZES_STRING
);
u
.
prop
=
engine
->
capture
->
get
(
CV_CAP_PROP_SUPPORTED_PREVIEW_SIZES_STRING
);
cv
::
Size
resolution
;
int
view_width
=
ANativeWindow_getWidth
(
app
->
window
);
int
view_height
=
ANativeWindow_getHeight
(
app
->
window
);
cv
::
Size
camera_resolution
;
if
(
u
.
name
)
if
(
u
.
name
)
resolution
=
calcOptimalResolution
(
u
.
name
,
camera_resolution
=
calc_optimal_camera_resolution
(
u
.
name
,
640
,
480
);
ANativeWindow_getWidth
(
app
->
window
),
ANativeWindow_getHeight
(
app
->
window
));
else
else
{
{
LOGE
(
"Cannot get supported camera resolutions"
);
LOGE
(
"Cannot get supported camera
camera_
resolutions"
);
resolution
=
cv
::
Size
(
ANativeWindow_getWidth
(
app
->
window
),
camera_
resolution
=
cv
::
Size
(
ANativeWindow_getWidth
(
app
->
window
),
ANativeWindow_getHeight
(
app
->
window
));
ANativeWindow_getHeight
(
app
->
window
));
}
}
if
((
resolution
.
width
!=
0
)
&&
(
resolution
.
height
!=
0
))
if
((
camera_resolution
.
width
!=
0
)
&&
(
camera_
resolution
.
height
!=
0
))
{
{
engine
->
capture
->
set
(
CV_CAP_PROP_FRAME_WIDTH
,
resolution
.
width
);
engine
->
capture
->
set
(
CV_CAP_PROP_FRAME_WIDTH
,
camera_
resolution
.
width
);
engine
->
capture
->
set
(
CV_CAP_PROP_FRAME_HEIGHT
,
resolution
.
height
);
engine
->
capture
->
set
(
CV_CAP_PROP_FRAME_HEIGHT
,
camera_
resolution
.
height
);
}
}
if
(
ANativeWindow_setBuffersGeometry
(
app
->
window
,
resolution
.
width
,
float
scale
=
std
::
min
((
float
)
view_width
/
camera_resolution
.
width
,
resolution
.
height
,
WINDOW_FORMAT_RGBA_8888
)
<
0
)
(
float
)
view_height
/
camera_resolution
.
height
);
if
(
ANativeWindow_setBuffersGeometry
(
app
->
window
,
(
int
)(
view_width
/
scale
),
int
(
view_height
/
scale
),
WINDOW_FORMAT_RGBA_8888
)
<
0
)
{
{
LOGE
(
"Cannot set pixel format!"
);
LOGE
(
"Cannot set pixel format!"
);
return
;
return
;
}
}
LOGI
(
"Camera initialized at resoution %dx%d"
,
resolution
.
width
,
resolution
.
height
);
LOGI
(
"Camera initialized at resoution %dx%d"
,
camera_resolution
.
width
,
camera_
resolution
.
height
);
}
}
break
;
break
;
case
APP_CMD_TERM_WINDOW
:
case
APP_CMD_TERM_WINDOW
:
...
@@ -158,9 +163,8 @@ void android_main(android_app* app)
...
@@ -158,9 +163,8 @@ void android_main(android_app* app)
engine
.
app
=
app
;
engine
.
app
=
app
;
float
fps
=
0
;
float
fps
=
0
;
cv
::
Mat
drawingFrame
;
cv
::
Mat
drawing_frame
;
bool
firstFrame
=
true
;
std
::
queue
<
int64
>
time_queue
;
std
::
queue
<
int64
>
timeQueue
;
// loop waiting for stuff to do.
// loop waiting for stuff to do.
while
(
1
)
while
(
1
)
...
@@ -189,34 +193,29 @@ void android_main(android_app* app)
...
@@ -189,34 +193,29 @@ void android_main(android_app* app)
int64
then
;
int64
then
;
int64
now
=
cv
::
getTickCount
();
int64
now
=
cv
::
getTickCount
();
time
Q
ueue
.
push
(
now
);
time
_q
ueue
.
push
(
now
);
// Capture frame from camera and draw it
// Capture frame from camera and draw it
if
(
!
engine
.
capture
.
empty
())
if
(
!
engine
.
capture
.
empty
())
{
{
if
(
engine
.
capture
->
grab
())
if
(
engine
.
capture
->
grab
())
{
engine
.
capture
->
retrieve
(
drawing_frame
,
CV_CAP_ANDROID_COLOR_FRAME_RGBA
);
engine
.
capture
->
retrieve
(
drawingFrame
,
CV_CAP_ANDROID_COLOR_FRAME_RGBA
);
// if (firstFrame)
// {
// firstFrame = false;
// engine.capture->set(CV_CAP_PROP_AUTOGRAB, 1);
// }
}
char
buffer
[
256
];
char
buffer
[
256
];
sprintf
(
buffer
,
"Display performance: %dx%d @ %.3f"
,
drawingFrame
.
cols
,
drawingFrame
.
rows
,
fps
);
sprintf
(
buffer
,
"Display performance: %dx%d @ %.3f"
,
drawing_frame
.
cols
,
drawing_frame
.
rows
,
fps
);
cv
::
putText
(
drawingFrame
,
std
::
string
(
buffer
),
cv
::
Point
(
8
,
64
),
cv
::
FONT_HERSHEY_COMPLEX_SMALL
,
1
,
cv
::
Scalar
(
0
,
255
,
0
,
255
));
cv
::
putText
(
drawing_frame
,
std
::
string
(
buffer
),
cv
::
Point
(
8
,
64
),
engine_draw_frame
(
&
engine
,
drawingFrame
);
cv
::
FONT_HERSHEY_COMPLEX_SMALL
,
1
,
cv
::
Scalar
(
0
,
255
,
0
,
255
));
engine_draw_frame
(
&
engine
,
drawing_frame
);
}
}
if
(
time
Q
ueue
.
size
()
>=
2
)
if
(
time
_q
ueue
.
size
()
>=
2
)
then
=
time
Q
ueue
.
front
();
then
=
time
_q
ueue
.
front
();
else
else
then
=
0
;
then
=
0
;
if
(
time
Q
ueue
.
size
()
>=
25
)
if
(
time
_q
ueue
.
size
()
>=
25
)
time
Q
ueue
.
pop
();
time
_q
ueue
.
pop
();
fps
=
1.
f
*
timeQueue
.
size
()
*
(
float
)
cv
::
getTickFrequency
()
/
(
float
)
(
now
-
then
);
fps
=
time_queue
.
size
()
*
(
float
)
cv
::
getTickFrequency
()
/
(
now
-
then
);
}
}
}
}
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