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框架包括支持各种照相机和照相机特性.允许在应用中捕捉图像和视频.本文档讨论了快速简单的图像视频捕捉方法,及为你的用户体验创建定制的照相机框架
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. 在你的应用中使用摄像头非常重要,所以你不让你的应用按装在没有摄像头的设备上,是吗?如果是,那你要在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.
你的应用怎样使用摄像头?你只是想捕捉一张照片或者视频剪辑,或者你想用一种新方法使用摄像头?若是快速捕捉图片或剪辑,考虑Using Existing Camera App.要开发定制的摄像特性,提取Building a Camera App 一节.
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.
应用产生的图像或者视频,只是想在你的应用中可见或者要共享,以便其他的应用比如Gallery或其他的多媒体及社会的应用能使用它们?你想让这些图像即使在你的应用被卸载掉后仍可用吗?提取 Saving Media Files来看如何实现这些选项
The Android framework supports capturing images and video through the Camera
API or camera Intent
. Here are the relevant classes:
Android框架支持通过Camera
API或者Intent
,来捕捉图像和视频,下面是相关的类:
Camera
SurfaceView
MediaRecorder
Intent
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
or MediaStore.ACTION_VIDEO_CAPTURE
意图
,
在不直接使用对象的情况下
,捕捉图像和视频
.
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.
在使用CameraAPI开发应用前,你应先确保你的manifest文件中有使用摄像硬件和其他相关特点的相应的声明.
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.关于摄像特性列表,请看manifest的Features Reference .
Adding camera features to your manifest causes Android Market 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 Android Market, see Android Market and Feature-Based Filtering.
添加特征到你的manifest文件,会导致Android Market阻止你的应用安装在那些没有包括摄像机或者不支持你指定特点的摄像设备上.关于Android Market使用特征过滤的详细信息,请看 Android Market and Feature-Based Filtering
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
:为了相应的操作,如果你的应用能使用某个摄像头或者摄像的某个特征,但是不是必须的.你应该在manifest 中,包括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. 如果你的应用想要存储图像和视频到设备的外部存储器,你必须在manifest中指定如下
<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: 如果你的应的tags图像带有GPS定位信息,你必须请求定位权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
For more information about getting user location, see Obtaining User Location.关于获取用户定位的详细信息,请看 Obtaining User Location
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:调用摄像意图的过程,一般如下步骤:
Compose a Camera Intent - Create an Intent
that requests an image or video, using one of these intent types: 使用如下意图类型的一种,创建一个请求图像和视频的意图:
MediaStore.ACTION_IMAGE_CAPTURE
- Intent action type for requesting an image from an existing camera application. 从一个存在的摄像应用请求一个图片的类型的意图.
MediaStore.ACTION_VIDEO_CAPTURE
- Intent action type for requesting a video from an existing camera application. 从一个存在的摄像应用请求一个视频的类型的意图.
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. 使用startActivityForResult()
方法去执行摄像机意图.在你启动这个意图后,摄像机应用用户界面出现在设备屏幕上,并且用户可以白照和获取视频了.
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. 在你的应用中设置onActivityResult()
方法,来接回调收和摄像意图的数据.当用户获取了图像或者视频(或者取消),系统调用这个方法.
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.
这个设置,请求一个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()
方法参考Saving Media Files 中的代码
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private Uri fileUri; @Override 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 = 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()
方法被执行,用户会看到一个camera应用界面.在用户拍照完成后,用户界面返回到你的应用,并且你必须截获onActivityResult()
方法,接收意图的结果并且继续执行你的应用.关于如何接收一个完成的意图,看 Receiving camera intent result.
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.
这个设置,请求一个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. 这个值可以是0,表示最小质量和最小文件大小,或1,表示最高质量和最大文件大小.
MediaStore.EXTRA_DURATION_LIMIT
- Set this value to limit the length, in seconds, of the video being captured. 以秒为单位,限制视频从开始捕捉时起的时间长度
MediaStore.EXTRA_SIZE_LIMIT
- Set this value to limit the file size, in bytes, of the video being captured. 以字节为单位,限制限制视频从开始捕捉时起的文件大小,
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()
方法参考Saving Media Files 中的代码
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200; private Uri fileUri; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //create new 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()
方法被执行,用户会看到一个camera应用界面.在用户拍照完成后,用户界面返回到你的应用,并且你必须截获onActivityResult()
方法,接收意图的结果并且继续执行你的应用.关于如何接收一个完成的意图,看接下来的部分
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()
方法来获取上面部分的例子中的 image camera intent 或 video camera intent 意图的返回结果.
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200; @Override 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 Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show(); } else if (resultCode == RESULT_CANCELED) { // User cancelled the image capture } else { // 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 Toast.makeText(this, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show(); } else if (resultCode == RESULT_CANCELED) { // User cancelled the video capture } else { // 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.
一旦你的应用接收到一个成功的结果,你的应用就可以访问指定位置捕获的图像和视频.
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.
一些开发者可能要将摄像用户界面定制成它的应用的外观样式,或者提供特别的特性,创建一个定制的摄像activity,比使用一个意图要更多的代码.但是它更能为你的用户提供更棒的体验.
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.
Camera硬件是共享资源,所以必须精心管理,以便你的应用不要与其他想用Camera的应用相冲突.下面部分讨论了如何检测摄像硬件,怎样请求访问摄像头,怎样捕捉图像和视频,及当你的应用使用完后如何释放摄像头.
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.如果你的应用没有适当的释放摄像头,所有接下来访问摄像头的尝试(包括你自己的应用访问),将会失败,并且可能导致你的或其他的应用关闭 .
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:
如果在manifest声明中,指出你的应用不必要求一个报像头,在运行时,你应该检测是否有摄像头可用.要执行这个检测,使用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 return true; } else { // no camera on this device return false; } }
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设备,可以有多个摄像头,比如,back-facing后面的摄像头用于照像,front-facing前面的用于视频电话.Android2.3(API Level 9)以后允许你检测设备上可用设像头的个数.使用Camera.getNumberOfCameras()
方法.
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 ).
如果你检测到你的应用运行设备上有一个摄像头,你必须通过获取一个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. */ public static Camera getCameraInstance(){ Camera c = null; try { c = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } return c; // returns null if camera is unavailable }
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.当使用方法时,总是要检测异常.如果摄像头正在使用,或者不存在时,不检测异常会导致系统关闭你的应用.
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或更高的版本的设备上,你可以用方法访问指定的摄像头.当设备上有多个摄像头上,上面的例子只访问第一个,即back-facing 摄像头.
Once you obtain access to a camera, you can get further information about its capabilties 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 Level 9以上的版本时,用Camera.getCameraInfo()
方法检测某个摄像头是在前或者在后,及图像的方向.
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 */ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera; // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 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. try { 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. } 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 return; } // stop preview before making changes try { 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 try { 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()
方法中使用任意值.
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.
一个camera预览视图类,比上面的例子,必须和其他的拍照和拍视频的控制界面一起放进一个活动布局,这部分教你如何为一个预览视图建立一个基本的布局.
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.
下面这个布局代码提供一个用于显示camear预览的一个基本视图.在这个例子中,FrameLayout
元素是camera预览的容器.使用这个布局可使得其也的附加图像和控制,可以复盖在即时camera图像上面
<?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" /> <Button 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.
在大多数设备上,摄像的默认朝向是横向的.例子中的布局是垂直布局,下面代码把它改成横向布局.为了简单的优化你的camear预览,你应该在你的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()
.
一个camera预览视图,不必是横向模式,从Android2.2开始,你可以使用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.
上面的例子,展示了在你的camera预览的活动中,加入你的预览类到FrameLayout
元素中,
>>>>>>>>>>续<<<<<<<<<<<