Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
云duo
android-camera2video
提交
ec99302b
A
android-camera2video
项目概览
云duo
/
android-camera2video
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
android-camera2video
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ec99302b
编写于
9月 04, 2015
作者:
G
google-automerger
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Auto-update
上级
5b445a57
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
126 addition
and
11 deletion
+126
-11
Application/build.gradle
Application/build.gradle
+2
-1
Application/src/main/java/com/example/android/camera2video/Camera2VideoFragment.java
...om/example/android/camera2video/Camera2VideoFragment.java
+122
-10
Application/src/main/res/values/strings.xml
Application/src/main/res/values/strings.xml
+2
-0
未找到文件。
Application/build.gradle
浏览文件 @
ec99302b
...
...
@@ -19,6 +19,7 @@ dependencies {
compile
"com.android.support:support-v4:23.0.0"
compile
"com.android.support:support-v13:23.0.0"
compile
"com.android.support:cardview-v7:23.0.0"
compile
'com.android.support:appcompat-v7:23.0.0'
}
// The sample build uses multiple directories to
...
...
@@ -35,7 +36,7 @@ android {
defaultConfig
{
minSdkVersion
21
targetSdkVersion
2
2
targetSdkVersion
2
3
}
compileOptions
{
...
...
Application/src/main/java/com/example/android/camera2video/Camera2VideoFragment.java
浏览文件 @
ec99302b
...
...
@@ -16,6 +16,7 @@
package
com.example.android.camera2video
;
import
android.Manifest
;
import
android.app.Activity
;
import
android.app.AlertDialog
;
import
android.app.Dialog
;
...
...
@@ -23,6 +24,7 @@ import android.app.DialogFragment;
import
android.app.Fragment
;
import
android.content.Context
;
import
android.content.DialogInterface
;
import
android.content.pm.PackageManager
;
import
android.content.res.Configuration
;
import
android.graphics.Matrix
;
import
android.graphics.RectF
;
...
...
@@ -39,6 +41,9 @@ import android.media.MediaRecorder;
import
android.os.Bundle
;
import
android.os.Handler
;
import
android.os.HandlerThread
;
import
android.support.annotation.NonNull
;
import
android.support.v13.app.FragmentCompat
;
import
android.support.v4.app.ActivityCompat
;
import
android.util.Log
;
import
android.util.Size
;
import
android.util.SparseIntArray
;
...
...
@@ -59,11 +64,19 @@ import java.util.List;
import
java.util.concurrent.Semaphore
;
import
java.util.concurrent.TimeUnit
;
public
class
Camera2VideoFragment
extends
Fragment
implements
View
.
OnClickListener
{
public
class
Camera2VideoFragment
extends
Fragment
implements
View
.
OnClickListener
,
FragmentCompat
.
OnRequestPermissionsResultCallback
{
private
static
final
SparseIntArray
ORIENTATIONS
=
new
SparseIntArray
();
private
static
final
String
TAG
=
"Camera2VideoFragment"
;
private
static
final
int
REQUEST_VIDEO_PERMISSIONS
=
1
;
private
static
final
String
FRAGMENT_DIALOG
=
"dialog"
;
private
static
final
String
[]
VIDEO_PERMISSIONS
=
{
Manifest
.
permission
.
CAMERA
,
Manifest
.
permission
.
RECORD_AUDIO
,
};
static
{
ORIENTATIONS
.
append
(
Surface
.
ROTATION_0
,
90
);
...
...
@@ -88,7 +101,8 @@ public class Camera2VideoFragment extends Fragment implements View.OnClickListen
private
CameraDevice
mCameraDevice
;
/**
* A reference to the current {@link android.hardware.camera2.CameraCaptureSession} for preview.
* A reference to the current {@link android.hardware.camera2.CameraCaptureSession} for
* preview.
*/
private
CameraCaptureSession
mPreviewSession
;
...
...
@@ -198,14 +212,12 @@ public class Camera2VideoFragment extends Fragment implements View.OnClickListen
};
public
static
Camera2VideoFragment
newInstance
()
{
Camera2VideoFragment
fragment
=
new
Camera2VideoFragment
();
fragment
.
setRetainInstance
(
true
);
return
fragment
;
return
new
Camera2VideoFragment
();
}
/**
* In this sample, we choose a video size with 3x4 aspect ratio. Also, we don't use sizes
larger
* than 1080p, since MediaRecorder cannot handle such a high-resolution video.
* In this sample, we choose a video size with 3x4 aspect ratio. Also, we don't use sizes
*
larger
than 1080p, since MediaRecorder cannot handle such a high-resolution video.
*
* @param choices The list of available sizes
* @return The video size
...
...
@@ -331,16 +343,79 @@ public class Camera2VideoFragment extends Fragment implements View.OnClickListen
}
}
/**
* Gets whether you should show UI with rationale for requesting permissions.
*
* @param permissions The permissions your app wants to request.
* @return Whether you can show permission rationale UI.
*/
private
boolean
shouldShowRequestPermissionRationale
(
String
[]
permissions
)
{
for
(
String
permission
:
permissions
)
{
if
(
FragmentCompat
.
shouldShowRequestPermissionRationale
(
this
,
permission
))
{
return
true
;
}
}
return
false
;
}
/**
* Requests permissions needed for recording video.
*/
private
void
requestVideoPermissions
()
{
if
(
shouldShowRequestPermissionRationale
(
VIDEO_PERMISSIONS
))
{
new
ConfirmationDialog
().
show
(
getChildFragmentManager
(),
FRAGMENT_DIALOG
);
}
else
{
FragmentCompat
.
requestPermissions
(
this
,
VIDEO_PERMISSIONS
,
REQUEST_VIDEO_PERMISSIONS
);
}
}
@Override
public
void
onRequestPermissionsResult
(
int
requestCode
,
@NonNull
String
[]
permissions
,
@NonNull
int
[]
grantResults
)
{
Log
.
d
(
TAG
,
"onRequestPermissionsResult"
);
if
(
requestCode
==
REQUEST_VIDEO_PERMISSIONS
)
{
if
(
grantResults
.
length
==
VIDEO_PERMISSIONS
.
length
)
{
for
(
int
result
:
grantResults
)
{
if
(
result
!=
PackageManager
.
PERMISSION_GRANTED
)
{
ErrorDialog
.
newInstance
(
getString
(
R
.
string
.
permission_request
))
.
show
(
getChildFragmentManager
(),
FRAGMENT_DIALOG
);
break
;
}
}
}
else
{
ErrorDialog
.
newInstance
(
getString
(
R
.
string
.
permission_request
))
.
show
(
getChildFragmentManager
(),
FRAGMENT_DIALOG
);
}
}
else
{
super
.
onRequestPermissionsResult
(
requestCode
,
permissions
,
grantResults
);
}
}
private
boolean
hasPermissionsGranted
(
String
[]
permissions
)
{
for
(
String
permission
:
permissions
)
{
if
(
ActivityCompat
.
checkSelfPermission
(
getActivity
(),
permission
)
!=
PackageManager
.
PERMISSION_GRANTED
)
{
return
false
;
}
}
return
true
;
}
/**
* Tries to open a {@link CameraDevice}. The result is listened by `mStateCallback`.
*/
private
void
openCamera
(
int
width
,
int
height
)
{
if
(!
hasPermissionsGranted
(
VIDEO_PERMISSIONS
))
{
requestVideoPermissions
();
return
;
}
final
Activity
activity
=
getActivity
();
if
(
null
==
activity
||
activity
.
isFinishing
())
{
return
;
}
CameraManager
manager
=
(
CameraManager
)
activity
.
getSystemService
(
Context
.
CAMERA_SERVICE
);
try
{
Log
.
d
(
TAG
,
"tryAcquire"
);
if
(!
mCameraOpenCloseLock
.
tryAcquire
(
2500
,
TimeUnit
.
MILLISECONDS
))
{
throw
new
RuntimeException
(
"Time out waiting to lock camera opening."
);
}
...
...
@@ -369,7 +444,8 @@ public class Camera2VideoFragment extends Fragment implements View.OnClickListen
}
catch
(
NullPointerException
e
)
{
// Currently an NPE is thrown when the Camera2API is used but not supported on the
// device this code runs.
new
ErrorDialog
().
show
(
getFragmentManager
(),
"dialog"
);
ErrorDialog
.
newInstance
(
getString
(
R
.
string
.
camera_error
))
.
show
(
getChildFragmentManager
(),
FRAGMENT_DIALOG
);
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
"Interrupted while trying to lock camera opening."
);
}
...
...
@@ -559,11 +635,21 @@ public class Camera2VideoFragment extends Fragment implements View.OnClickListen
public
static
class
ErrorDialog
extends
DialogFragment
{
private
static
final
String
ARG_MESSAGE
=
"message"
;
public
static
ErrorDialog
newInstance
(
String
message
)
{
ErrorDialog
dialog
=
new
ErrorDialog
();
Bundle
args
=
new
Bundle
();
args
.
putString
(
ARG_MESSAGE
,
message
);
dialog
.
setArguments
(
args
);
return
dialog
;
}
@Override
public
Dialog
onCreateDialog
(
Bundle
savedInstanceState
)
{
final
Activity
activity
=
getActivity
();
return
new
AlertDialog
.
Builder
(
activity
)
.
setMessage
(
"This device doesn't support Camera2 API."
)
.
setMessage
(
getArguments
().
getString
(
ARG_MESSAGE
)
)
.
setPositiveButton
(
android
.
R
.
string
.
ok
,
new
DialogInterface
.
OnClickListener
()
{
@Override
public
void
onClick
(
DialogInterface
dialogInterface
,
int
i
)
{
...
...
@@ -575,4 +661,30 @@ public class Camera2VideoFragment extends Fragment implements View.OnClickListen
}
public
static
class
ConfirmationDialog
extends
DialogFragment
{
@Override
public
Dialog
onCreateDialog
(
Bundle
savedInstanceState
)
{
final
Fragment
parent
=
getParentFragment
();
return
new
AlertDialog
.
Builder
(
getActivity
())
.
setMessage
(
R
.
string
.
permission_request
)
.
setPositiveButton
(
android
.
R
.
string
.
ok
,
new
DialogInterface
.
OnClickListener
()
{
@Override
public
void
onClick
(
DialogInterface
dialog
,
int
which
)
{
FragmentCompat
.
requestPermissions
(
parent
,
VIDEO_PERMISSIONS
,
REQUEST_VIDEO_PERMISSIONS
);
}
})
.
setNegativeButton
(
android
.
R
.
string
.
cancel
,
new
DialogInterface
.
OnClickListener
()
{
@Override
public
void
onClick
(
DialogInterface
dialog
,
int
which
)
{
parent
.
getActivity
().
finish
();
}
})
.
create
();
}
}
}
Application/src/main/res/values/strings.xml
浏览文件 @
ec99302b
...
...
@@ -3,4 +3,6 @@
<string
name=
"record"
>
Record
</string>
<string
name=
"stop"
>
Stop
</string>
<string
name=
"description_info"
>
Info
</string>
<string
name=
"permission_request"
>
This sample needs permission for camera and audio recording.
</string>
<string
name=
"camera_error"
>
This device doesn\'t support Camera2 API.
</string>
</resources>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录