怎样取消激活comwww.d8video.comsupp管理权限功能

微软 Bing 搜索 - 国内版必应国内版国际版下载手机必应本文目录:
3.1 文件/目录的权限
3.1.1 文件的权限
每个文件都有其所有者(u:user)、所属组(g:group)和其他人(o:other)对它的操作权限,a:all则同时代表这3者。权限包括读(r:read)、写(w:write)、执行(x:execute)。在不同类型的文件上读、写、执行权限的体现有所不同,所以目录权限和普通文件权限要区分开来。
在普通文件上:
r:可读,可以使用类似cat等命令查看文件内容;读是文件的最基本权限,没有读权限,普通文件的一切操作行为都被限制。
w:可写,可以编辑此文件;
x:可执行,表示文件可由特定的解释器解释并运行。可以理解为windows中的可执行程序或批处理脚本,双击就能运行起来的文件。
在目录上:
r:可以对目录执行ls以列出目录内的所有文件;读是文件的最基本权限,没有读权限,目录的一切操作行为都被限制。
w:可以在此目录创建或删除文件/子目录;
x:可进入此目录,可使用ls -l查看文件的详细信息。可以理解为windows中双击就进入目录的动作。
如果目录没有x权限,其他人将无法查看目录内文件属性(只能查看到文件类型和文件名,至于为什么,见后文),所以一般目录都要有x权限。而如果只有执行却没有读权限,则权限拒绝。
一般来说,普通文件的默认权限是644(没有执行权限),目录的默认权限是755(必须有执行权限,否则进不去),链接文件的权限是777。当然,默认文件的权限设置方法是可以通过umask值来改变的。
3.1.2 权限的表示方式
权限的模式有两种体现:数字体现方式和字符体现方式。
权限的数字表示:"-"代表没有权限,用0表示。
&&&&&&&&&&&&&&& r-----4
&&&&&&&&&&&&&&& w-----2
&&&&&&&&&&&&&&& x-----1
例如:rwx rw- r--对应的数字权限是764,732代表的权限数值表示为rwx -wx -w-。
3.1.3 chmod修改权限
能够修改权限的人只有文件所有者和超级管理员。
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... num_mode FILE...
chmod [OPTION]... --reference=RFILE FILE...
选项说明:
--reference=RFILE:引用某文件的权限作为权限值
-R:递归修改,只对当前已存在的文件有效
(1). 使用数值方式修改权限
shell& chmod 755 /tmp/a.txt
(2). 使用字符方式修改权限
由于权限属性附在文件所有者、所属组和其它上,它们三者都有独立的权限位,所有者使用字母"u"表示,所属组使用"g"来表示,其他使用"o"来表示,而字母"a"同时表示它们三者。所以使用字符方式修改权限时,需要指定操作谁的权限。
chmod [ugoa][+ - =] [权限字符] 文件/目录名
"+"是加上权限,"-"是减去权限,"="是直接设置权限
[root@xuexi tmp]# chmod u-x,g-x,o-x test
# 将ugo都去掉x权限,等价于chmod -x test
[root@xuexi tmp]# chmod a+x test
# 为ugo都加上x权限,等价于chmod +x test
3.1.4 chgrp
更改文件和目录的所属组,要求组已经存在。
注意,对于链接文件而言,修改组的作用对象是链接的源文件,而非链接文件本身。
chgrp [OPTION]... GROUP FILE...
chgrp [OPTION]... --reference=RFILE FILE..
选项说明:
-R:递归修改
--reference=dest_file
file_list:引用某文件的group作为文件列表的组,即将file文件列表的组改为dest_file的组
3.1.5 chown
chown可以修改文件所有者和所属组。
注意,对于链接文件而言,默认不会穿过链接修改源文件,而是直接修改链接文件本身,这和chgrp的默认是不一样的。
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... [OWNER][.[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...
选项说明:
--from=CURRENT_OWNER:CURRENT_GROUP:只修改当前所有者或所属组为此处指定的值的文件
--reference=RFILE:引用某文件的所有者和所属组的值作为新的所有者和所属组
-R:递归修改。注意,当指定-R时,且同时指定下面某一个选项时对链接文件有不同的行为
-H:如果chown的文件参数是一个链接到目录的链接文件,则穿过此链接文件修改其源目录的所有者和所属组
-L:目录中遇到的所有链接文件都穿越过去,修改它们的源文件的所有者和所属组
-P:不进行任何穿越,只修改链接文件本身的所有者和所属组。(这是默认值)
这3项若同时指定多项时,则最后一项生效
chown指定所有者和所属组的方式有两种,使用冒号和点。
shell& chown root.root test
shell& chown root:root test
shell& chown root test
# 只修改所有者
shell& chown :root test
# 自修改组
shell& chown .root test
3.2 实现权限的本质
涉及文件系统的知识点,若不理解,可以先看看文件系统的内容。此处是以ext4文件系统为例的,在其他文件系统上结果可能会有些不一样(centos 7上使用xfs文件系统时结果可能就不一样),但本质是一样的。
不同的权限表示对文件具有不同能力,如读写执行(rwx)权限,但是它是怎么实现的呢?描述文件权限的数据放在哪里呢?
首先,权限的元数据放在inode中,严格地说是放在inode table中,因为每个块组的所有inode组成一个inode table。在inode table中使用一列来存放数字型的权限,比如某文件的权限为644。每次用户要对文件进行操作时系统都会先查看权限,确认该用户是否有对应的权限来执行操作。当然,inode table一般都已经加载到内存中,所以每次查询权限的资源消耗是非常小的。
无论是读、写还是执行权限,所体现出来的能力究其本质都是因为它作用在对应文件的data block上。
3.2.1 读权限(r)
对普通文件具有读权限表示的是具有读取该文件内容的能力,对目录具有读权限表示具有浏览该目录中文件或子目录的能力。其本质都是具有读取其data block的能力。
对于普通文件而言,能够读取文件的data block,而普通文件的data block存储的直接就是数据本身, 所以对普通文件具有读权限表示能够读取文件内容。
对于目录文件而言,能够读取目录的data block,而目录文件的data block存储的内容包括但不限于:目录中文件的inode号(并非直接存储,而是存储指向inode table中该inode号的指针)以及这些文件的文件类型、文件名。所以能够读取目录的data block表示仅能获取到这些信息。
目录的data block内容示例如下:
shell& mkdir -p /mydata/data/testdir/subdir
# 创建testdir测试目录和其子目录subdir
shell& touch /mydata/data/testdir/a.log
# 再在testdir下创建一个普通文件
shell& chmod 754 /mydata/data/testdir
# 将testdir设置为对其他人只有读权限
然后切换到普通用户查看testdir目录的内容。
shell& su - wangwu
shell& ll -ai /mydata/data/testdir/
ls: cannot access /mydata/data/testdir/..: Permission denied
ls: cannot access /mydata/data/testdir/a.log: Permission denied
ls: cannot access /mydata/data/testdir/subdir: Permission denied
ls: cannot access /mydata/data/testdir/.: Permission denied
? d????????? ? ? ? ?
? d????????? ? ? ? ?
? -????????? ? ? ? ?
? d????????? ? ? ? ?
从结果中看出,testdir下的文件名和文件类型是能够读取的,但是其他属性都不能读取到。而且也读取不到inode号,因为它并没有直接存储inode号,而是存储了指向Inode号的指针,要定位到指针的指向需要执行权限。
3.2.2 执行权限(x)
执行权限表示的是能够执行。如何执行?执行这个词不是很好解释,可以简单的类比Windows中的双击行为。例如对目录双击就能进入到目录,对批处理文件双击就能运行(有专门的解释器解释),对可执行程序双击就能运行等。
当然,读权限是文件的最基本权限,执行权限能正常运行必须得配有读权限。
对目录有执行权限,表示可以通过目录的data block中指向文件inode号的指针定位到inode table中该文件的inode信息,所以可以显示出这些文件的全部属性信息。
3.2.3 写权限(w)
写权限很简单,就是能够将数据写入分配到的data block,即磁盘中。同样,写权限要能正常工作,必须配有读权限,且对目录的写权限需要配合执行权限。
对目录文件具有写权限,表示能够创建和删除文件,实质是能够在目录的data block中创建或删除关于待操作文件的记录;
对普通文件具有写权限,实质是能够改写该文件的data block。
还是要说明的是,对文件有写权限不代表能够删除该文件,因为删除文件是要在目录的data block中删除该文件的记录,也就是说删除权限是在目录中定义的。
所以,对目录文件和普通文件而言,读、写、执行权限它们的依赖关系如下图所示。
3.3 umask说明
umask值用于设置用户在创建文件时的默认权限。对于root用户(实际上是UID小于200的user),系统默认的umask值是022;对于普通用户和系统用户,系统默认的umask值是002。
默认它们的设置是写在/etc/profile和/etc/bashrc两个环境配置文件中。
shell& grep -C 5 -R 'umask 002'
/etc | grep 'umask 022'
/etc/bashrc-
/etc/csh.cshrc-
/etc/profile-
相关设置项如下:
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
执行umask命令可以查看当前用户的umask值。
[root@xuexi tmp]# umask
[longshuai@xuexi tmp]$ umask
执行umask num可以临时修改umask值为num,但这是临时的,要永久有效,需要写入到环境配置文件中,至于写入到/etc/profile、/etc/bashrc、~/.bashrc还是~/.bash_profile中,看你自己的需求了。不过一般来说,不会去永久修改umask值,只会在特殊条件下临时修改下umask值。
umask是如何决定创建文件的默认权限的呢?
如果创建的是目录,则使用777-umask值,如root的umask=022,则root创建目录时该目录的默认权限为777-022=755,而普通用户创建目录时,权限为777-002=775.
如果创建的是普通文件,在Linux中,深入贯彻了一点:文件默认不应该有执行权限,否则是危险的。所以在计算时,可能会和想象中的结果不一样。如果umask的三位都为偶数,则直接使用666去减掉umask值,因为6减去一个偶数还是偶数,任何位都不可能会有执行权限。如root创建普通文件时默认权限为666-022=644,而普通用户创建普通文件时默认权限为666-002=664。
如果umask值某一位为奇数,则666减去umask值后再在奇数位上加1。如umask=021时,创建文件时默认权限为666-021=645,在奇数位上加1,则为646。
[longshuai@xuexi tmp]$ umask 021
[longshuai@xuexi tmp]$ touch b.txt
[longshuai@xuexi tmp]$ ls -l b.txt
-rw-r--rw- 1 longshuai longshuai 0 Jun
7 12:02 b.txt
总之计算出后默认都是没有执行权限的。
3.4 文件的扩展ACL权限
在计算机相关领域,所有的ACL(access control list)都表示访问控制列表。
文件的owner/group/others的权限就是一种ACL,它们是基本的ACL。很多时候,只通过这3个权限位是无法完全合理设置权限问题的,例如如何仅设置某单个用户具有什么权限。这时候需要使用扩展ACL。
扩展ACL是一种特殊权限,它是文件系统上功能,用于解决所有者、所属组和其他这三个权限位无法合理设置单个用户权限的问题。所以,扩展ACL可以针对单一使用者,单一档案或目录里的默认权限进行r,w,x的权限规范。
需要明确的是,扩展ACL是文件系统上的功能,且工作在内核,默认在ext4/xfs上都已开启。
在下文中,都直接以ACL来表示代替扩展ACL的称呼。
3.4.1 查看文件系统是否开启ACL功能
对于ext家族的文件系统来说,要查看是否开启acl功能,使用dumpe2fs导出文件系统属性即可。
shell& dumpe2fs -h /dev/sda2 | grep -i acl
dumpe2fs 1.41.12 (17-May-2010)
Default mount options:
user_xattr acl
对于xfs文件系统,则没有直接的命令可以输出它的相关信息,需要使用dmesg来查看。其实无需关注它,因为默认xfs会开启acl功能。
shell& dmesg | grep -i acl
1.465903] systemd[1]: systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
2.517705] SGI XFS with ACLs, security attributes, no debug enabled
开启ACL功能后,不代表就使用ACL功能。是否使用该功能,不同文件系统控制方法不一样,对于ext家族来说,通过mount挂载选项来控制,而对于xfs文件系统,mount命令根本不支持acl参数(xfs文件系统如何关闭或启用的方法本人也不知道)。
3.4.2 设置和查看ACL
设置使用setfacl命令。
setfacl [options] u:[用户列表]:[rwx] 目录/文件名
# 对用户设置使用u
setfacl [options] g:[组列表]:[rwx]
目录/文件名
# 对组设置使用g
选项说明:
-m:设定ACL权限(modify)
-x:删除指定的ACL权限,可以指定用户、组和文件来删除(remove)
-M:写了ACL条目的文件,将从此文件中读取ACL条目,需要配合-m,所以-M指定的是modify file
-X:写了ACL条目的文件,将从此文件中读取ACL条目,需要配合-x,所以-X指定的是remove file
-n:不重置mask
-b:删除所有的ACL权限
-d:设定默认ACL权限,只对目录有效,设置后子目录(文件)继承默认ACL,只对未来文件 有效
-k:删除默认ACL权限
-R:递归设定ACL权限,只对目录有效,只对已有文件有效
查看使用getfacl命令
getfacl filename
案例:假设现有目录/data/videos专门存放视频,其中有一个a.avi的介绍性视频。该目录的权限是750。现在有一个新用户加入,但要求该用户对该目录只有查看的权限,且只能看其中一部视频a.avi,另外还要求该用户在此目录下没有创建和删除文件的权限。
1.准备相关环境。
shell& mkdir -p /data/videos
shell& chmod 750 /data/videos
shell& touch /data/videos/{a,b}.avi
shell& echo "xxx" &/data/videos/a.avi
shell& echo "xxx" &/data/videos/b.avi
shell& chown -R root.root /data/videos
2.首先设置用户longshuai对/data/videos目录有读和执行权限。
shell& setfacl -m u:longshuai:rx /data/videos
3.现在longshuai对/data/videos目录下的所有文件都有执行权限,因为默认文件的权限为644。要设置longshuai只对a.avi有读权限,先设置所有文件的权限都为不可读。
shell& chmod 640 /data/videos/*
4.然后再单独设置a.avi的读权限。
shell& setfacl -m u:longshuai:r /data/videos/a.avi
到此就设置完成了。查看/data/videos/和/data/videos/a.avi上的ACL信息。
shell& getfacl /data/videos/
getfacl: Removing leading '/' from absolute path names
# file: data/videos/
# owner: root
# group: root
user:longshuai:r-x
# 用户longshuai在此文件上的权限是r-x
group::r-x
other::---
shell& getfacl /data/videos/a.avi
getfacl: Removing leading '/' from absolute path names
# file: data/videos/a.avi
# owner: root
# group: root
user:longshuai:r--
# 用户longshuai在此文件上的权限是r--
group::r--
other::---
3.4.3 ACL:mask
设置mask后会将mask权限与已有的acl权限进行与计算,计算后的结果会成为新的ACL权限。
设定mask的方式为:
setfacl -m m:[rwx] 目录/文件名
注意:默认每次设置文件的acl都会重置mask为此次给定的用户的值。既然如此,要如何控制文件上的acl呢?如果一个文件上要设置多个用户的acl,重置mask后就会对已有用户的acl重新计算,而使得acl权限得不到有效的控制。使用setfacl的"-n"选项,它表示此次设置不会重置mask值。
当前的acl权限:
shell& getfacl /data/videos
getfacl: Removing leading '/' from absolute path names
# file: data/videos
# owner: root
# group: root
user:longshuai:rwx
group::r-x
other::---
设置mask值为rx。
shell& setfacl -m m:rx /data/videos
shell& getfacl /data/videos
getfacl: Removing leading '/' from absolute path names
# file: data/videos
# owner: root
# group: root
user:longshuai:rwx
#effective:r-x
group::r-x
other::---
设置mask后,它提示有效权限是r-x。这是rwx和r-x做与运算之后的结果。
再设置longshuai的acl为rwx,然后查看mask,会发现mask也被重置为rwx。
shell& setfacl -m u:longshuai:rwx /data/videos
shell& getfacl
/data/videos
getfacl: Removing leading '/' from absolute path names
# file: data/videos
# owner: root
# group: root
user:longshuai:rwx
group::r-x
other::---
所以,在设置文件的acl时,要使用-n选项来禁止重置mask。
shell& setfacl -m m:rx /data/videos
shell& setfacl -n -m u:longshuai:rwx /data/videos
shell& getfacl
/data/videos
getfacl: Removing leading '/' from absolute path names
# file: data/videos
# owner: root
# group: root
user:longshuai:rwx
#effective:r-x
group::r-x
other::---
3.4.4 设置递归和默认ACL权限
递归ACL权限只对目录里已有文件有效,默认权限只对未来目录里的文件有效。
设置递归ACL权限:
setfacl -m u:username:[rwx] -R 目录名
# -R选项只能放在后面。
设置默认ACL权限:
setfacl -m d:u:username:[rwx] 目录名
3.4.5 删除ACL权限
setfacl -x u:用户名 文件名
# 删除指定用户ACL
setfacl -x g:组名 文件名
# 删除指定组名ACL
setfacl -b 文件名
# 指定文件删除ACL,会删除所有ACL
3.5 文件隐藏属性
chattr:change file attributes
lsattr:list file attributes
chattr [+ - =] [ai] 文件或目录名
常用的参数是a(append,追加)和i(immutable,不可更改),其他参数略。
设置了a参数时,文件中将只能增加内容,不能删除数据,且不能打开文件进行任何编辑,哪怕是追加内容也不可以,所以像sed等需要打开文件的再写入数据的工具也无法操作成功。文件也不能被删除。只有root才能设置。
设置了i参数时,文件将被锁定,不能向其中增删改内容,也不能删除修改文件等各种动作。只有root才能设置。可以将其理解为设置了i后,文件将是永恒不变的了,谁都不能动它。
例如,对/etc/shadow文件设置i属性,任何用户包括root将不能修改密码,而且也不能创建用户。
shell& chattr +i /etc/shadow
此时如果新建一个用户。
shell& useradd newlongsuai
shell& useradd: cannot open /etc/shadow
# 提示文件不能打开,被锁定了
lsattr查看文件设置的隐藏属性。
shell& lsattr /etc/shadow
----i--------e- /etc/shadow
# i属性说明被锁定了,e是另一种文件属性,忽略它
删除隐藏属性:
shell& chattr -i /etc/shadow
shell& lsattr /etc/shadow
-------------e- /etc/shadow
再来一例:
shell& chattr +a test1.txt
# 对test1.txt设置a隐藏属性
shell& echo 1234&&test1.txt
# 追加内容是允许的行为
shell& cat /dev/null &test1.txt
# 但是清空文件内容是不允许的
-bash: test1.txt: Operation not permitted
3.6 suid/sgid/sbit
3.6.1 suid
suid只针对可执行文件,即二进制文件。它的作用是对某个命令(可执行文件)授予所有者的权限,命令执行完成权限就消失。一般是提权为root权限。
例如/etc/shadow文件所有人都没有权限(root除外),其他用户连看都不允许。
shell& ls -l /etc/shadow
----------. 1 root root 752 Apr
8 12:42 /etc/shadow
但是他们却能修改自己的密码,说明他们一定有一定的权限。这个权限就是suid控制的。
shell& ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 Feb 22
2012 /usr/bin/passwd
其中的"s"权限就是suid,它出现在所有者位置上(是root),其他用户执行passwd命令时,会暂时拥有所有者位的rwx权限,也就是root的权限,所以能向/etc/shadow写入数据。
suid必须和x配合,如果没有x配合,则该suid是空suid,仍然没有执行命令的权限,所有者都没有了x权限,suid依赖于它所以更不可能有x权限。空的suid权限使用大写的"S"表示。
数字4代表suid,如4755。
3.6.2 sgid
针对二进制文件和目录。
针对二进制文件时,权限升级为命令的所属组权限。
针对目录时,目录中所建立的文件或子目录的组将继承默认父目录组,其本质还是提升为目录所属组的权限。此时目录应该要有rx权限,普通用户才能进入目录,如果普通用户有w权限,新建的文件和目录则以父目录组为默认组。
以2代表sgid,如2755,和suid组合如6755。
3.6.3 sbit
只对目录有效。对目录设置sbit,将使得目录里的文件只有所有者能删除,即使其他用户在此目录上有rwx权限,即使是root用户。
以1代表sbit。
补充:suid/sgid/sbit的标志位都作用在x位,当原来的x位有x权限时,这些权限位则为s/s/t,如果没有x权限,则变为S/S/T。例如,/tmp目录的权限有个t位,使得该目录里的文件只有其所有者本身能删除。
回到大纲:
转载请注明出处:
注:若您觉得这篇文章还不错请点击下右下角的推荐,有了您的支持才能激发作者更大的写作热情,非常感谢!
阅读(...) 评论()当前位置:&&
本页文章导读:
&&&&?怎么去掉listview headview的 divider &&&&&& 如何去掉listview headview的 divider
在项目中要求把listView 加一个headView 但headView 不能出现分隔线,找了很多办法。
才知道android listview有一个设置可以去掉
mListView01.setHeaderDividersEnabled(false);
同理.........&&&&? 【通译】(85)照相机 &&&&&& 【翻译】(85)照相机
【翻译】(85)照相机
http://developer.android.com/guide/topics/media/camera.html
http://developer.android.com/guide/topics/media/camera.html
-------------------------------
照相机.........&&&&? objective-c 内存储器释放备忘 转载&&&&&& objective-c 内存释放备忘 转载
//string1 将被自动释放NSString* string1 = [NSString string];//必须在用完后手工释放NSString* string2 = [[NSString alloc] init]; [string2 release];Typically, each class gets two files: a header f.........
[1]怎么去掉listview headview的 divider
&&&&来源:&互联网& 发布时间:&
如何去掉listview headview的 divider
在项目中要求把listView 加一个headView 但headView 不能出现分隔线,找了很多办法。
才知道android listview有一个设置可以去掉
mListView01.setHeaderDividersEnabled(false);
同理 footerview 也一样有setFooterDividersEnabled(false)
[2] 【通译】(85)照相机
&&&&来源:&互联网& 发布时间:&
【翻译】(85)照相机
【翻译】(85)照相机
http://developer.android.com/guide/topics/media/camera.html
http://developer.android.com/guide/topics/media/camera.html
-------------------------------
-------------------------------
In this document
* Considerations 考虑
* The Basics 基础
* Manifest Declarations 清单声明
* Using Existing Camera Apps 使用现存照相机应用
* Image capture intent 图片捕捉意图
* Video capture intent 视频捕捉意图
* Receiving camera intent result 接收照相机意图结果
* Building a Camera App 构建一个照相机应用
* Detecting camera hardware 检测照相机硬件
* Accessing cameras 访问照相机
* Checking camera features 检查照相机特性
* Creating a preview class 创建一个预览类
* Placing preview in a layout 在一个布局中放置预览
* Capturing pictures 捕捉相片
* Capturing videos 捕捉视频
* Releasing the camera 释放照相机
* Saving Media Files 保存媒体文件
* Camera Features 照相机特性
* Checking feature availability 检查特性可用性
* Using camera features 使用照相机特性
* Metering and focus areas 测量和焦点区域
* Face detection 人脸检测
* Time lapse video 慢速视频(注:录影的帧率比正常要慢很多的视频,定时视频)
Key Classes
SurfaceView
MediaRecorder
Media Playback 媒体回放
Data Storage 数据存储
-------------------------------
The Android framework includes support for various cameras and camera features available on devices, allowing you to capture pictures and videos in your applications. This document discusses a quick, simple approach to image and video capture and outlines an advanced approach for creating custom camera experiences for your users.
Android框架包含对各种照相机和在设备上可用的照相机特性的支持,允许你在你的应用程序中捕捉照片和视频。本文讨论图片和视频捕捉的一个快速、简单的方法以及概述为你的用户创建自定义照相机体验的一种高级方法。
-------------------------------
Considerations
Before enabling your application to use cameras on Android devices, you should consider a few questions about how your app intends to use this hardware feature.
在使能你的应用程序以在Android设备上使用照相机时,你应该考虑关于你的应用打算如何使用这个硬件特性的一些问题。
* Camera Requirement - Is the use of a camera so important to your application that you do not want your application installed on a device that does not have a camera? If so, you should declare the camera requirement in your manifest.
* 照相机要求(注:需要)——照相机的使用对于你的应用程序来说是否太重要以致于你不希望你的应用程序安装在没有一个照相机的设备上?如果那样的话,你应该在你的清单中声明照相机要求。
* Quick Picture or Customized Camera - How will your application use the camera? Are you just interested in snapping a quick picture or video clip, or will your application provide a new way to use cameras? For a getting a quick snap or clip, consider Using Existing Camera Apps. For developing a customized camera feature, check out the Building a Camera App section.
* 快速照片或被定制的照相机——你的应用程序将如何使用照相机?你是否只是感兴趣于拍摄快速照片或视频剪辑,还是你的应用程序将提供一个新的方式来使用照相机?为了获得一个快速拍摄或剪辑,请考虑使用现存照相机应用。为了开发一个被定制的照相机特性,请查看构建一个照相机应用章节。
* Storage - Are the images or videos your application generates intended to be only visible to your application or shared so that other applications such as Gallery or other media and social apps can use them? Do you want the pictures and videos to be available even if your application is uninstalled? Check out the Saving Media Files section to see how to implement these options.
* 存储——你的应用程序生成的照片或视频是否打算只对于你的应用程序可见还是被共享使其它应用程序诸如画廊或其它媒体和社交应用可以使用它们?你是否希望照片和视频是可用的即便你的应用程序被卸载?请查看保存媒体文件章节以了解如何实现这些选择。
-------------------------------
The Basics
The Android framework supports capturing images and video through the Camera API or camera Intent. Here are the relevant classes:
Android框架支持通过Camera的API或照相机Intent来捕捉图片和视频。这里有一些相关的类:
This class is the primary API for controlling device cameras. This class is used to take pictures or videos when you are building a camera application.
这个类是用于控制设备照相机的主要API。此类被用于照相或录视频,当你正在构建一个照相机应用程序时。
SurfaceView
This class is used to present a live camera preview to the user.
这个类被用于向用户呈现一个现场照相机预览。
MediaRecorder
This class is used to record video from the camera.
这个类被用于从照相机中记录视频。
An intent action type of MediaStore.ACTION_IMAGE_CAPTURE or MediaStore.ACTION_VIDEO_CAPTURE can be used to capture images or videos without directly using the Camera object.
一个MediaStore.ACTION_IMAGE_CAPTURE或MediaStore.ACTION_VIDEO_CAPTURE的意图动作类型可以被用于捕捉图片或视频而不直接地使用Camera对象。
-------------------------------
Manifest Declarations
Before starting development on your application with the Camera API, you should make sure your manifest has the appropriate declarations to allow use of camera hardware and other related features.
在开始使用照相机API在你的应用程序上的开发时,你应该确保你的清单拥有正确的声明以允许照相机硬件和其它相关特性的使用。
* Camera Permission - Your application must request permission to use a device camera.
* 照相机权限——你的应用程序必须请求权限以使用一个设备照相机。
-------------------------------
&uses-permission android:name="android.permission.CAMERA" /&
-------------------------------
-------------------------------
Note: If you are using the camera via an intent, your application does not need to request this permission.
注意:如果你正在通过一个意图使用照相机,你的应用程序不需要请求此权限。
-------------------------------
* Camera Features - Your application must also declare use of camera features, for example:
* 照相机特性——你的应用程序还必须声明照相机特性的使用,例如:
-------------------------------
&uses-feature android:name="android.hardware.camera" /&
-------------------------------
For a list of camera features, see the manifest Features Reference.
想获得一个照相机特性的列表,请参见清单特性参考手册。
Adding camera features to your manifest causes Google Play to prevent your application from being installed to devices that do not include a camera or do not support the camera features you specify. For more information about using feature-based filtering with Google Play, see Google Play and Feature-Based Filtering.
添加照相机特性到你的清单导致Google Play阻止你的应用程序被安装到不包含一个照相机或不支持你指定的照相机特性的设备。想获得关于使用Google Play的基于特性过滤的更多信息,请参见Google Play和基于特性过滤。
If your application can use a camera or camera feature for proper operation, but does not require it, you should specify this in the manifest by including the android:required attribute, and setting it to false:
如果你的应用程序可以使用一个照相机或照相机特性以获得正确的操作,但并不一定需要它,那么你应该在清单中通过包含android:required属性,并且设置它为false来指定它。
-------------------------------
&uses-feature android:name="android.hardware.camera" android:required="false" /&
-------------------------------
* Storage Permission - If your application saves images or videos to the device's external storage (SD Card), you must also specify this in the manifest.
* 存储权限——如果你的应用程序保存图片或视频到设备的外部存储(SD卡),你必须在清单中指定它。
-------------------------------
&uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&
-------------------------------
* Audio Recording Permission - For recording audio with video capture, your application must request the audio capture permission.
* 音频记录权限——对于伴随视频捕捉的音频记录,你的应用程序必须请求音频捕捉权限。
-------------------------------
&uses-permission android:name="android.permission.RECORD_AUDIO" /&
-------------------------------
* Location Permission - If your application tags images with GPS location information, you must request location permission:
* 位置权限——如果你的应用程序用GPS位置信息标签图片,你必须请求位置权限:
-------------------------------
&uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&
-------------------------------
For more information about getting user location, see Obtaining User Location.
想获得关于获得用户位置的更多信息,请参见获取用户位置。
-------------------------------
Using Existing Camera Apps
使用现存照相机应用
A quick way to enable taking pictures or videos in your application without a lot of extra code is to use an Intent to invoke an existing Android camera application. A camera intent makes a request to capture a picture or video clip through an existing camera app and then returns control back to your application. This section shows you how to capture an image or video using this technique.
在你的应用程序中使能照相或录视频而不使用大量额外代码的一种快捷方法是使用一个Intent来调用一个现存Android照相机应用程序。一个照相机意图作出一个请求以通过一个现存照相机应用捕捉一个相片或视频剪辑,然后把控制权返回给你的应用程序。这个章节向你展示如何使用这项技术捕捉一个图片或视频。
The procedure for invoking a camera intent follows these general steps:
调用一个照相机意图的过程遵循这些通常的步骤:
1. Compose a Camera Intent - Create an Intent that requests an image or video, using one of these intent types:
1. 创作一个照相机意图——创建一个Intent,它请求一个图片或视频,使用这些意图类型中的其中一个:
* MediaStore.ACTION_IMAGE_CAPTURE - Intent action type for requesting an image from an existing camera application.
* MediaStore.ACTION_IMAGE_CAPTURE——意图动作类型,用于从一个现存照相机应用程序中请求一个图片。
* MediaStore.ACTION_VIDEO_CAPTURE - Intent action type for requesting a video from an existing camera application.
* MediaStore.ACTION_VIDEO_CAPTURE——意图动作类型,用于从一个现存照相机应用程序中请求一个视频。
2. Start the Camera Intent - Use the startActivityForResult() method to execute the camera intent. After you start the intent, the Camera application user interface appears on the device screen and the user can take a picture or video.
2. 启动照相机意图——使用startActivityForResult()方法来执行照相机意图。在你启动意图之后,照相机应用程序用户界面出现在设备屏幕上,而用户可以照相或录视频。
3. Receive the Intent Result - Set up an onActivityResult() method in your application to receive the callback and data from the camera intent. When the user finishes taking a picture or video (or cancels the operation), the system calls this method.
3. 接收意图结果——在你的应用程序中配置一个onActivityResult()方法以从照相机意图中接收回调和数据。当用户完成照相或录视频时(或取消操作),系统调用这个方法。
Image capture intent
图片捕捉意图
Capturing images using a camera intent is quick way to enable your application to take pictures with minimal coding. An image capture intent can include the following extra information:
使用一个照相机意图捕捉图片是快捷方法以使能你的应用程序用最小的代码来照相。一个图片捕捉意图可以包含以下额外信息:
* MediaStore.EXTRA_OUTPUT - This setting requires a Uri object specifying a path and file name where you'd like to save the picture. This setting is optional but strongly recommended. If you do not specify this value, the camera application saves the requested picture in the default location with a default name, specified in the returned intent's Intent.getData() field.
* MediaStore.EXTRA_OUTPUT——则个设置需要一个Uri对象指定一个路径和文件名,你喜欢保存相片在那里。这个设置是可选的但是强烈建议的。如果你不指定这个值,那么照相机应用程序保存被请求的相片在默认位置带一个默认的名称,在返回的意图的Intent.getData()域中被指定。
The following example demonstrates how to construct a image capture intent and execute it. The getOutputMediaFileUri() method in this example refers to the sample code shown in Saving Media Files.
以下示例演示如何构建一个图片捕捉意图并且执行它。在这个示例中getOutputMediaFileUri()方法引用了在保存媒体文件中展示的示例代码。
-------------------------------
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileU
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create Intent to take a picture and return control to the calling application
// 创建Intent以照相并返回控制给调用方应用程序
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image 创建一个文件以保存图片
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name 设置图片文件名
// start the image capture Intent
// 启动图片捕捉意图
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
-------------------------------
When the startActivityForResult() method is executed, users see a camera application interface. After the user finishes taking a picture (or cancels the operation), the user interface returns to your application, and you must intercept the onActivityResult() method to receive the result of the intent and continue your application execution. For information on how to receive the completed intent, see Receiving camera intent result.
当startActivityForResult()方法被执行时,用户看到一个应用程序界面。在用户完成照相(或取消该操作)之后,用户界面返回到你的应用程序,而你必须拦截onActivityResult()方法以接收意图的结果并且继续你的应用程序的执行。想获得关于如何接收完成意图的信息,请参见接收照相机意图结果。
Video capture intent
视频捕捉意图
Capturing video using a camera intent is a quick way to enable your application to take videos with minimal coding. A video capture intent can include the following extra information:
使用一个照相机意图捕捉视频是一个快捷方法以使能你的应用程序来用最小的代码录视频。一个视频捕捉意图可以包含以下额外信息:
* MediaStore.EXTRA_OUTPUT - This setting requires a Uri specifying a path and file name where you'd like to save the video. This setting is optional but strongly recommended. If you do not specify this value, the Camera application saves the requested video in the default location with a default name, specified in the returned intent's Intent.getData() field.
* MediaStore.EXTRA_OUTPUT——这个设置需要一个Uri指定一个路径和文件名,你喜欢保存视频在那里。这个设置是可选的,但是强烈建议。如果你不指定这个值,那么照相机应用程序保存被请求的视频在默认位置带有一个默认名称,在返回的意图的Intent.getData()域中被指定。
* MediaStore.EXTRA_VIDEO_QUALITY - This value can be 0 for lowest quality and smallest file size or 1 for highest quality and larger file size.
* MediaStore.EXTRA_VIDEO_QUALITY——这个值对于最低质量和最小文件大小可以为0,或者对于最高质量和较大文件大小来说为1。
* MediaStore.EXTRA_DURATION_LIMIT - Set this value to limit the length, in seconds, of the video being captured.
* MediaStore.EXTRA_DURATION_LIMIT——设置这个值以限制正在被捕捉的视频的长度,单位为秒。
* MediaStore.EXTRA_SIZE_LIMIT - Set this value to limit the file size, in bytes, of the video being captured.
* MediaStore.EXTRA_SIZE_LIMIT——设置这个值以限制正在被捕捉的视频的文件大小,单位为字节。
The following example demonstrates how to construct a video capture intent and execute it. The getOutputMediaFileUri() method in this example refers to the sample code shown in Saving Media Files.
以下示例展示如何构造一个视频捕捉意图并执行它。在这个实例中中的getOutputMediaFileUri()方法引用在保存媒体文件中展示的示例代码。
-------------------------------
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
private Uri fileU
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create new Intent
//创建新的Intent对象
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// create a file to save the video 创建一个文件以保存视频
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// set the image file name 设置图片文件的名称
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high 设置视频图片质量为高
// start the Video Capture Intent
// 启动视频捕捉意图
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
-------------------------------
When the startActivityForResult() method is executed, users see a modified camera application interface. After the user finishes taking a video (or cancels the operation), the user interface returns to your application, and you must intercept the onActivityResult() method to receive the result of the intent and continue your application execution. For information on how to receive the completed intent, see the next section.
当startActivityForResult()方法被执行时,用户看见一个被修改的照相机应用程序界面。在用户完成录一个视频(或取消该操作)之后,用户界面返回到你的应用程序,而你必须拦截onActivityResult()方法已接收意图的结果和继续你的应用程序的执行。想获得关于如何接收完成意图的信息,请参见下一章节。
Receiving camera intent result
接收照相机意图结果
Once you have constructed and executed an image or video camera intent, your application must be configured to receive the result of the intent. This section shows you how to intercept the callback from a camera intent so your application can do further processing of the captured image or video.
一旦你已经构造和执行一个图片或视频照相机意图,你的应用程序必须被配置以接收意图的结果。这个章节向你展示如何从一个照相机意图中拦截回调,使你的应用程序可以进行对被捕捉图片或视频的进一步处理。
In order to receive the result of an intent, you must override the onActivityResult() in the activity that started the intent. The following example demonstrates how to override onActivityResult() to capture the result of the image camera intent or video camera intent examples shown in the previous sections.
为了接收一个意图的结果,你必须在启动意图的活动中覆盖onActivityResult()。以下示例演示如何覆盖onActivityResult()以捕捉在前面章节中展示的图片照相机意图或视频照相机意图示例的结果。
-------------------------------
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
// 图片被捕捉并保存到在Intent中指定的fileUri
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
// 用户取消图片捕捉
// Image capture failed, advise user
// 图片捕捉失败,告知(注:建议)用户
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
// 视频被捕捉并保存到Intent中指定的fileUri
Toast.makeText(this, "Video saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
// 用户取消视频捕捉
// Video capture failed, advise user
// 视频捕捉失败,告知(注:建议)用户
-------------------------------
Once your activity receives a successful result, the captured image or video is available in the specified location for your application to access.
一旦你的活动接收到一个成功结果,被捕捉的图片或视频在指定位置是可用的,供你的应用程序访问。
-------------------------------
Building a Camera App
构建一个照相机应用
Some developers may require a camera user interface that is customized to the look of their application or provides special features. Creating a customized camera activity requires more code than using an intent, but it can provide a more compelling experience for your users.
一些开发者可能需要一个照相机用户界面,它被定制为他们的应用程序的外观或提供特殊特性。创建一个被定制的照相机活动需要比使用意图更多的代码,但它可以为你的用户提供一个更引人注目的体验。
The general steps for creating a custom camera interface for your application are as follows:
为你的应用程序创建一个被定制的照相机接口(注:界面)的通常步骤如下:
* Detect and Access Camera - Create code to check for the existence of cameras and request access.
* 检测和访问照相机——创建代码以检查照相机的存在和请求访问权。
* Create a Preview Class - Create a camera preview class that extends SurfaceView and implements the SurfaceHolder interface. This class previews the live images from the camera.
* 创建一个预览类——创建一个照相机预览类,它扩展SurfaceView并且实现SurfaceHolder接口。这个类预览来自照相机的现场图片。
* Build a Preview Layout - Once you have the camera preview class, create a view layout that incorporates the preview and the user interface controls you want.
* 构建一个预览布局——一旦你拥有照相机预览类,请创建一个视图布局,它包含你想要的预览和用户界面控件。
* Setup Listeners for Capture - Connect listeners for your interface controls to start image or video capture in response to user actions, such as pressing a button.
* 为捕捉配置监听器——连接你的界面控件的监听器以启动图片或视频捕捉来响应用户动作,诸如按下一个按钮。
* Capture and Save Files - Setup the code for capturing pictures or videos and saving the output.
* 捕捉和保存文件——配置用于捕捉照片或视频和保存输出的代码。
* Release the Camera - After using the camera, your application must properly release it for use by other applications.
* 释放照相机——在使用照相机后你的应用程序必须正确地释放它,以便让其它应用程序使用它。
Camera hardware is a shared resource that must be carefully managed so your application does not collide with other applications that may also want to use it. The following sections discusses how to detect camera hardware, how to request access to a camera, how to capture pictures or video and how to release the camera when your application is done using it.
照相机硬件是一个共享资源,它必须被小心地管理,使你的应用程序不与也可能希望使用它的其它应用程序冲突。以下章节讨论如何检测照相机硬件,如何请求访问一个照相机,如何捕捉相片或视频,以及当你的应用程序完成使用它时如何释放照相机。
-------------------------------
Caution: Remember to release the Camera object by calling the Camera.release() when your application is done using it! If your application does not properly release the camera, all subsequent attempts to access the camera, including those by your own application, will fail and may cause your or other applications to be shut down.
警告:记得当你的应用程序用完它时,通过调用Camera.release()释放Camera对象!如果你的应用程序不正确地释放照相机,那么所有随后访问照相机的尝试,包括那些你自己的应用程序作出的尝试,将失败并且可能导致你的或其它应用程序被关闭。
-------------------------------
Detecting camera hardware
检测照相机硬件
If your application does not specifically require a camera using a manifest declaration, you should check to see if a camera is available at runtime. To perform this check, use the PackageManager.hasSystemFeature() method, as shown in the example code below:
如果你的应用程序不特别地使用一个清单声明来需要一个照相机,你应该在运行时检查以了解照相机是否可用。为了执行这种检查,请使用PackageManager.hasSystemFeature()方法,正如下面的示例代码中所示:
-------------------------------
/** Check if this device has a camera */
/** 检查这台设备是否拥有照相机 */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
// 这台设备拥有照相机
// no camera on this device
// 这台设备上没有照相机
-------------------------------
Android devices can have multiple cameras, for example a back-facing camera for photography and a front-facing camera for video calls. Android 2.3 (API Level 9) and later allows you to check the number of cameras available on a device using the Camera.getNumberOfCameras() method.
Android设备可以拥有多个照相机,例如一个后置照相机用于照相和一个前置照相机用于视频通话。Android 2.3(API级别9)和更新的版本允许你使用Camera.getNumberOfCameras()方法检查一台设备上可用的照相机数量。
Accessing cameras
访问照相机
If you have determined that the device on which your application is running has a camera, you must request to access it by getting an instance of Camera (unless you are using an intent to access the camera).
如果你已经确定在你的应用程序运行在的设备上拥有一个照相机,你必须通过获得一个Camera实例来请求访问它(除非你正在使用一个意图来访问照相机)。
To access the primary camera, use the Camera.open() method and be sure to catch any exceptions, as shown in the code below:
为了访问主照相机,请使用Camera.open()方法并确保捕捉任意异常,正如下面代码中所示:
-------------------------------
/** A safe way to get an instance of the Camera object. */
/** 一种安全的方式以获得一个Camera对象的实例。 */
public static Camera getCameraInstance(){
Camera c =
c = Camera.open(); // attempt to get a Camera instance 尝试获得一个Camera实例
catch (Exception e){
// Camera is not available (in use or does not exist)
// 照相机不可用(正在使用或不存在)
// returns null if camera is unavailable 返回null如果照相机不可用
-------------------------------
-------------------------------
Caution: Always check for exceptions when using Camera.open(). Failing to check for exceptions if the camera is in use or does not exist will cause your application to be shut down by the system.
警告:当使用Camera.open()时总是检查异常。检查异常失败,如果照相机正在使用或不存在,将导致你的应用程序被系统关闭。
-------------------------------
On devices running Android 2.3 (API Level 9) or higher, you can access specific cameras using Camera.open(int). The example code above will access the first, back-facing camera on a device with more than one camera.
在运行Android 2.3(API级别9)或更高的设备上,你可以使用Camera.open(int)访问指定照相机。上面的示例代码将访问带多于一个照相机的设备上第一个背向照相机(注:后置摄像头,一般叫Rear camera,是指照相手机背后的摄像头,是相对Front Facing Camera而言的。可能比正常的数码相机要省电些)。
Checking camera features
检查照相机特性
Once you obtain access to a camera, you can get further information about its capabilities using the Camera.getParameters() method and checking the returned Camera.Parameters object for supported capabilities. When using API Level 9 or higher, use the Camera.getCameraInfo() to determine if a camera is on the front or back of the device, and the orientation of the image.
一旦你取得对一个照相机的访问权,你可以使用Camera.getParameters()方法获得关于它的能力的进一步信息并且针对被支持的能力检查返回的Camera.Parameters对象。当使用API级别9或更高时,请使用Camera.getCameraInfo()以确定一个找星际是在设备的前方还是在后方,以及图片的方向。
Creating a preview class
创建一个预览类
For users to effectively take pictures or video, they must be able to see what the device camera sees. A camera preview class is a SurfaceView that can display the live image data coming from a camera, so users can frame and capture a picture or video.
为了让用户有效地照相或录视频,他们必须有能力了解设备照相机看到什么。一个照相机预览类是一个SurfaceView,它可以显示来自一个照相机的现场图片数据,那样用户可以框定(注:待考)和捕捉一个相片或视频。
The following example code demonstrates how to create a basic camera preview class that can be included in a View layout. This class implements SurfaceHolder.Callback in order to capture the callback events for creating and destroying the view, which are needed for assigning the camera preview input.
以下示例代码演示如何创建一个基本照相机预览类,它可以被包含在一个View布局中。这个类实现SurfaceHolder.Callback以便捕捉创建和销毁视图的回调事件,需要用它们来赋予(注:指定)照相机预览的输入。
-------------------------------
/** A basic Camera preview class */
/** 一个基本的Camera预览类 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mH
private Camera mC
public CameraPreview(Context context, Camera camera) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
// 安装一个SurfaceHolder.Callback那样当底层表面被创建和销毁时我们被通知。
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
// 被废弃的设置,但在3.0之前的Android版本上是必需的
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
// Surface已经被创建,现在告诉照相机在哪里绘画预览。
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
// 空。小心在你的活动中释放Camera预览
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
// 如果你的预览可以改变或旋转,请小心这里的那些事件。
// 确保在改变大小或重新格式化它之前停止预览。
if (mHolder.getSurface() == null){
// preview surface does not exist
// 预览表面不存在
// stop preview before making changes
// 在作出改变之前停止预览
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
// 忽略:尝试停止一个不存在的预览
// set preview size and make any resize, rotate or
// reformatting changes here
// 在这里设置预览大小并作出任意的改变大小,旋转或
// 重新格式化的改变。
// start preview with new settings
// 用新的设置开始预览
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
-------------------------------
If you want to set a specific size for your camera preview, set this in the surfaceChanged() method as noted in the comments above. When setting preview size, you must use values from getSupportedPreviewSizes(). Do not set arbitrary values in the setPreviewSize() method.
如果你希望为你的照相机预览设置一个特定的大小,请在surfaceChanged()方法中设置它,正如上面的注释中所注明的。当设置预览大小时,你必须使用来自getSupportedPreviewSizes()的值。不要在setPreviewSize()方法里设置任意值。
Placing preview in a layout
在布局中放置预览
A camera preview class, such as the example shown in the previous section, must be placed in the layout of an activity along with other user interface controls for taking a picture or video. This section shows you how to build a basic layout and activity for the preview.
一个照相机预览类,诸如在前一章节中所示的例子,必须被放置在一个活动的布局内伴随其他用户界面控件,用于照相或录视频。这个章节向你展示如何为该预览构建一个基本布局和活动。
The following layout code provides a very basic view that can be used to display a camera preview. In this example, the FrameLayout element is meant to be the container for the camera preview class. This layout type is used so that additional picture information or controls can be overlayed on the live camera preview images.
以下布局代码提供一个非常基本的视图,它可以被用于显示一个照相机预览。在这个示例中,FrameLayout元素打算做照相机预览类的容器。这个布局类型被使用,使额外的相片信息或控件(注:控制)可以在现场照相机预览图片上被覆盖。
-------------------------------
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
&FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
&/LinearLayout&
-------------------------------
On most devices, the default orientation of the camera preview is landscape. This example layout specifies a horizontal (landscape) layout and the code below fixes the orientation of the application to landscape. For simplicity in rendering a camera preview, you should change your application's preview activity orientation to landscape by adding the following to your manifest.
在大多数设备上,照相机预览的默认方向是宽屏方向。这个示例布局指定一个水平(宽屏)的布局,而下面的代码固定(注:修复)应用程序的方向为水平方向。为了简化渲染一个照相机预览,你应该改变你的应用程序的预览活动方向为宽屏方向,通过添加以下内容到你的清单中。
-------------------------------
&activity android:name=".CameraActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"&
&!-- configure this activity to use landscape orientation --&
&!-- 配置这个活动以使用宽屏方向 --&
&intent-filter&
&action android:name="android.intent.action.MAIN" /&
&category android:name="android.intent.category.LAUNCHER" /&
&/intent-filter&
&/activity&
-------------------------------
-------------------------------
Note: A camera preview does not have to be in landscape mode. Starting in Android 2.2 (API Level 8), you can use the setDisplayOrientation() method to set the rotation of the preview image. In order to change preview orientation as the user re-orients the phone, within the surfaceChanged() method of your preview class, first stop the preview with Camera.stopPreview() change the orientation and then start the preview again with Camera.startPreview().
注意:一个照相机预览不一定处于宽屏模式。在Android 2.2开始(API级别8),你可以使用setDisplayOrientation()方法以设置预览图片的旋转。为了在用户改变电话方向时改变预览方向,在你的预览类的surfaceChanged()方法内,首先用Camera.stopPreview()停止预览,改变方向,然后再次用Camera.startPreview()启动预览。
-------------------------------
In the activity for your camera view, add your preview class to the FrameLayout element shown in the example above. Your camera activity must also ensure that it releases the camera when it is paused or shut down. The following example shows how to modify a camera activity to attach the preview class shown in Creating a preview class.
在你的照相机视图的活动中,添加你的预览类到上面示例中展示的FrameLayout元素。你的照相机活动还必须确保它释放照相机,当它被暂停或被关闭。以下示例展示如何修改照相机活动以依附在创建一个预览类中展示的预览类。
-------------------------------
public class CameraActivity extends Activity {
private Camera mC
private CameraPreview mP
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an instance of Camera
// 创建一个Camera实例
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
// 创建我们的预览视图并设置它作为我们的活动的内容。
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
preview.addView(mPreview);
-------------------------------
-------------------------------
Note: The getCameraInstance() method in the example above refers to the example method shown in Accessing cameras.
注意:在上面的示例中getCameraInstance()方法引用在访问照相机中展示的示例方法。
-------------------------------
Capturing pictures
Once you have built a preview class and a view layout in which to display it, you are ready to start capturing images with your application. In your application code, you must set up listeners for your user interface controls to respond to a user action by taking a picture.
一旦你已经构建一个预览类和一个在其中显示它的视图布局,你就准备好开始用你的应用程序捕捉图片。在你的应用程序代码中,你必须为你的用户界面控件配置监听器以响应由照相引发的用户动作(注:待考)。
In order to retrieve a picture, use the Camera.takePicture() method. This method takes three parameters which receive data from the camera. In order to receive data in a JPEG format, you must implement an Camera.PictureCallback interface to receive the image data and write it to a file. The following code shows a basic implementation of the Camera.PictureCallback interface to save an image received from the camera.
为了取出一个相片,请使用Camera.takePicture()方法。这个方法传入三个参数,它们从照相机中接收数据。为了接收JPEG格式的数据,你必须实现一个Camera.PictureCallback接口以接收图片数据和写入它到一个文件。以下代码展示Camera.PictureCallback接口的一个基本实现以保存一个接收自照相机的一个图片。
-------------------------------
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: " +
e.getMessage());
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
-------------------------------
Trigger capturing an image by calling the Camera.takePicture() method. The following example code shows how to call this method from a button View.OnClickListener.
通过调用Camera.takePicture()方法触发捕捉一个图片。以下示例代码展示如何从一个按钮View.OnClickListener中调用这个方法。
-------------------------------
// Add a listener to the Capture button
// 添加一个监听器到捕捉按钮
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// get an image from the camera
// 从照相机中获得一个图片
mCamera.takePicture(null, null, mPicture);
-------------------------------
-------------------------------
Note: The mPicture member in the following example refers to the example code above.
注意:在以下示例中mPicture成员引用上面的示例代码。
-------------------------------
-------------------------------
Caution: Remember to release the Camera object by calling the Camera.release() when your application is done using it! For information about how to release the camera, see Releasing the camera.
警告:记得通过调用Camera.release()释放Camera对象,当你的引用程序已经完成使用它时!想获得关于如何释放照相机的信息,请参见释放照相机。
-------------------------------
Capturing videos
Video capture using the Android framework requires careful management of the Camera object and coordination with the MediaRecorder class. When recording video with Camera, you must manage the Camera.lock() and Camera.unlock() calls to allow MediaRecorder access to the camera hardware, in addition to the Camera.open() and Camera.release() calls.
使用Android框架的视频捕捉需要Camera对象的小心管理以及与MediaRecorder类协调。当使用照相机记录视频时,你必须管理Camera.lock()和Camera.unlock()调用以允许MediaRecorder访问照相机硬件,除Camera.open()和Camera.release()调用以外。
-------------------------------
Note: Starting with Android 4.0 (API level 14), the Camera.lock() and Camera.unlock() calls are managed for you automatically.
注意:从Android 4.0(API级别14)开始,自动地为你管理Camera.lock()和Camera.unlock()调用。
-------------------------------
Unlike taking pictures with a device camera, capturing video requires a very particular call order. You must follow a specific order of execution to successfully prepare for and capture video with your application, as detailed below.
不像用一个设备照相机照相那样,捕捉视频需要一个非常特殊的调用顺序。你必须遵循一个特定执行次序以成功地准备和捕捉你的应用程序的视频,正如下面的详细说明那样。
1. Open Camera - Use the Camera.open() to get an instance of the camera object.
1. 打开照相机——使用Camera.open()以获得一个照相机对象的实例。
2. Connect Preview - Prepare a live camera image preview by connecting a SurfaceView to the camera using Camera.setPreviewDisplay().
2. 连接预览——准备一个现场照相机图片预览,通过使用Camera.setPreviewDisplay()连接一个SurfaceView到照相机。
3. Start Preview - Call Camera.startPreview() to begin displaying the live camera images.
3. 开始预览——调用Camera.startPreview()以开始显示现场照相机图片。
4. Start Recording Video - The following steps must be completed in order to successfully record video:
4. 开始记录视频——为了成功地记录视频,以下步骤必须被完成:
a. Unlock the Camera - Unlock the camera for use by MediaRecorder by calling Camera.unlock().
a. 解锁照相机——为了通过MediaRecorder来使用,通过调用Camera.unlock()解锁照相机。(注:待考)
b. Configure MediaRecorder - Call in the following MediaRecorder methods in this order. For more information, see the MediaRecorder reference documentation.
b. 配置MediaRecorder——在以下MediaRecorder方法中以这个次序来调用。想获得更多信息,请参见MediaRecorder参考文档。
1. setCamera() - Set the camera to be used for video capture, use your application's current instance of Camera.
1. setCamera()——设置照相机以被用于视频捕捉,使用你的应用程序的当前Camera实例。
2. setAudioSource() - Set the audio source, use MediaRecorder.AudioSource.CAMCORDER.
2. setAudioSource()——设置音频源,使用MediaRecorder.AudioSource.CAMCORDER。
3. setVideoSource() - Set the video source, use MediaRecorder.VideoSource.CAMERA.
3. setVideoSource()——设置视频源,使用MediaRecorder.VideoSource.CAMERA。
4. Set the video output format and encoding. For Android 2.2 (API Level 8) and higher, use the MediaRecorder.setProfile method, and get a profile instance using CamcorderProfile.get(). For versions of Android prior to 2.2, you must set the video output format and encoding parameters:
4. 设置视频输出格式和编码。对于Android 2.2(API级别8)和更高,请使用MediaRecorder.setProfile方法,并且使用CamcorderProfile.get()取得一个简介(注:剖析)实例。对于2.2之前的Android版本,你必须设置视频输出格式和编码参数:
i. setOutputFormat() - Set the output format, specify the default setting or MediaRecorder.OutputFormat.MPEG_4.
i. setOutputFormat()——设置输出格式,指定默认设置或MediaRecorder.OutputFormat.MPEG_4
ii. setAudioEncoder() - Set the sound encoding type, specify the default setting or MediaRecorder.AudioEncoder.AMR_NB.
ii. setAudioEncoder()——设置声音编码类型,指定默认设置或MediaRecorder.AudioEncoder.AMR_NB。
iii. setVideoEncoder() - Set the video encoding type, specify the default setting or MediaRecorder.VideoEncoder.MPEG_4_SP.
iii. setVideoEncoder()——设置视频编码类型,指定默认默认设置或MediaRecorder.VideoEncoder.MPEG_4_SP。
5. setOutputFile() - Set the output file, use getOutputMediaFile(MEDIA_TYPE_VIDEO).toString() from the example method in the Saving Media Files section.
5. setOutputFile()——设置输出文件,使用来自保存媒体文件章节中的示例方法的getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()。
6. setPreviewDisplay() - Specify the SurfaceView preview layout element for your application. Use the same object you specified for Connect Preview.
6. setPreviewDisplay()——指定你的应用程序的SurfaceView预览布局元素。使用你为连接预览所指定的相同对象。
-------------------------------
Caution: You must call these MediaRecorder configuration methods in this order, otherwise your application will encounter errors and the recording will fail.
警告:你必须按这个次序调用这些MediaRecorder配置方法,否则你的应用程序将遇到错误而且记录将失败。
-------------------------------
c. Prepare MediaRecorder - Prepare the MediaRecorder with provided configuration settings by calling MediaRecorder.prepare().
c. 准备MediaRecorder——通过调用MediaRecorder.prepare()用提供的配置设置准备MediaRecorder。
d. Start MediaRecorder - Start recording video by calling MediaRecorder.start().
d. 启动MediaRecorder——通过调用MediaRecorder.start()开始记录视频。
5. Stop Recording Video - Call the following methods in order, to successfully complete a video recording:
5. 停止记录视频——依次调用以下方法,以成功地完成一个视频记录:
a. Stop MediaRecorder - Stop recording video by calling MediaRecorder.stop().
a. 停止MediaRecorder——通过调用MediaRecorder.stop()停止记录视频。
b. Reset MediaRecorder - Optionally, remove the configuration settings from the recorder by calling MediaRecorder.reset().
b. 重置MediaRecorder——可选地,通过调用MediaRecorder.reset()从记录器中移除配置设置。
c. Release MediaRecorder - Release the MediaRecorder by calling MediaRecorder.release().
c. 释放MediaRecorder——通过调用MediaRecorder.release()释放MediaRecorder。
d. Lock the Camera - Lock the camera so that future MediaRecorder sessions can use it by calling Camera.lock(). Starting with Android 4.0 (API level 14), this call is not required unless the MediaRecorder.prepare() call fails.
d. 锁定照相机——锁定照相机,使得未来的MediaRecorder会话可以通过调用Camera.lock()来使用它。从Android 4.0(API级别14)开始,这个调用不是必需的,除非MediaRecorder.prepare()调用失败。
6. Stop the Preview - When your activity has finished using the camera, stop the preview using Camera.stopPreview().
6. 停止预览——当你的活动已经完成使用照相机,请使用Camera.stopPreview()停止预览。
7. Release Camera - Release the camera so that other applications can use it by calling Camera.release().
7. 释放照相机——通过调用Camera.release()释放照相机以使得其它应用程序可以使用它。
-------------------------------
Note: It is possible to use MediaRecorder without creating a camera preview first and skip the first few steps of this process. However, since users typically prefer to see a preview before starting a recording, that process is not discussed here.
注意:可以使用MediaRecorder而不首先创建一个照相机预览和跳过这个处理开头的几个步骤。然而,因为用户通常倾向于在开始一个记录前看到一个预览,所以那种处理不在这里讨论。
-------------------------------
-------------------------------
Tip: If your application is typically used for recording video, set setRecordingHint(boolean) to true prior to starting your preview. This setting can help reduce the time it takes to start recording.
提示:如果你的应用程序被惯常地用于记录视频,请在开始你的预览之前设置setRecordingHint(boolean)为true。这个设置可以有助于缩短它用于开始记录的时间。
-------------------------------
Configuring MediaRecorder
配置MediaRecorder
When using the MediaRecorder class to record video, you must perform configuration steps in a specific order and then call the MediaRecorder.prepare() method to check and implement the configuration. The following example code demonstrates how to properly configure and prepare the MediaRecorder class for video recording.
当使用MediaRecorder类来记录视频时,你必须以一个特定次序执行配置步骤,然后调用MediaRecorder.prepare()方法以检查和实现配置。以下示例代码演示如何为了视频记录而正确地配置和准备MediaRecorder类。
-------------------------------
private boolean prepareVideoRecorder(){
mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
// 步骤1:解锁并设置照相机到MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
// 步骤2:设置源
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
// 步骤3:设置一个CamcorderProfile(需要API级别8或更高)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
// 步骤4:设置输出文件
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
// 步骤5:设置预览输出
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
// 步骤6:准备被配置的MediaRecorder
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
-------------------------------
Prior to Android 2.2 (API Level 8), you must set the output format and encoding formats parameters directly, instead of using CamcorderProfile. This approach is demonstrated in the following code:
在Android 2.2(API级别8)开始,你必须直}

我要回帖

更多关于 suppport.huawei.com 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信