Android multimedia and camera details 7

Source: Internet
Author: User

Create a preview class
To allow users to effectively obtain images and videos, they must be able to see images in the camera. A camera preview class is a SurfaceView class that can display Real-Time Images in the camera, so users can frame and capture images or videos.


The Code in the following example demonstrates how to create a basic camera preview class that can be included by a viewlayout. This class implements SurfaceHolder. Callback to obtain the Callback event for creating and destroying a view. This view is used to assign the camera preview input.

[Java]
/** 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 will be notified when the underlying interface is created or destroyed.
MHolder = getHolder ();
MHolder. addCallback (this );
// Outdated settings, but versions earlier than Android are required.
MHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
}
 
Public void surfaceCreated (SurfaceHolder holder ){
// The page is created, and the camera is now notified when to draw a preview.
Try {
MCamera. setPreviewDisplay (holder );
MCamera. startPreview ();
} Catch (IOException e ){
Log. d (TAG, "Error setting camera preview:" + e. getMessage ());
}
}
 
Public void surfaceDestroyed (SurfaceHolder holder ){
// Null. Release the camera preview in your activity.
}
 
Public void surfaceChanged (SurfaceHolder holder, int format, int w, int h ){
// If your preview can be changed or rotated, be careful with these events.
// Ensure that the preview is stopped before the size or format is changed.
If (mHolder. getSurface () = null ){
// The preview page does not exist.
Return;
}
 
// Stop previewing before changing
Try {
MCamera. stopPreview ();
} Catch (Exception e ){
// Ignore: tried to stop a non-existent preview
}
 
// Set the preview size and perform all changes in size, rotation, or format.
 
// Start to use the new settings for preview.
Try {
MCamera. setPreviewDisplay (mHolder );
MCamera. startPreview ();
 
} Catch (Exception e ){
Log. d (TAG, "Error starting camera preview:" + e. getMessage ());
}
}
}
/** 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 will be notified when the underlying interface is created or destroyed.
MHolder = getHolder ();
MHolder. addCallback (this );
// Outdated settings, but versions earlier than Android are required.
MHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
}

Public void surfaceCreated (SurfaceHolder holder ){
// The page is created, and the camera is now notified when to draw a preview.
Try {
MCamera. setPreviewDisplay (holder );
MCamera. startPreview ();
} Catch (IOException e ){
Log. d (TAG, "Error setting camera preview:" + e. getMessage ());
}
}

Public void surfaceDestroyed (SurfaceHolder holder ){
// Null. Release the camera preview in your activity.
}

Public void surfaceChanged (SurfaceHolder holder, int format, int w, int h ){
// If your preview can be changed or rotated, be careful with these events.
// Ensure that the preview is stopped before the size or format is changed.
If (mHolder. getSurface () = null ){
// The preview page does not exist.
Return;
}

// Stop previewing before changing
Try {
MCamera. stopPreview ();
} Catch (Exception e ){
// Ignore: tried to stop a non-existent preview
}

// Set the preview size and perform all changes in size, rotation, or format.

// Start to use the new settings for preview.
Try {
MCamera. setPreviewDisplay (mHolder );
MCamera. startPreview ();

} Catch (Exception e ){
Log. d (TAG, "Error starting camera preview:" + e. getMessage ());
}
}
}

 

If you want to set a size for your camera preview, you should do so in the surfaceChanged () method. When setting the preview size, you must use getSupportedPreviewSizes () to obtain the correct size value. You cannot use setPreviewSize () to set any size value.

 


Place the preview view in layout.

A camera preview class, as shown in the previous example, must be put together with other user interface controls in layout to obtain images or videos. This section describes how to create a basic layout and activity for preview.

The following layout Code provides a very basic view, which can display a camera preview. in this example, the FrameLayout element is a container of the camera preview class. the layout type is used to display other image information or control controls that can overwrite the preview image.

[Html]
<? 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>
<? 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 orientation of the camera preview image is horizontal. in the preceding layout example, the horizontal layout is specified. to simply display the camera preview, you should specify the horizontal direction of your app activity in your manifest.

[Html]
<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>
<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: The camera preview is not required to be horizontal. from Android2.2 (API Level8), you can use the setDisplayOrientation () method to set the rotation of the preview image. to change the preview direction to the same direction as the current device, in the surfaceChanged () method of your preview class, call Camera first. stopPreview () Stop previewing, change the direction, and then use Camera. startPreview () Restart preview.

 


Add your preview class to the FrameLayout element in the preceding example in the activity you used for camera view. your camera activity must ensure that the camera object is released when it is paused or disabled. the following example demonstrates how to modify a camera activity to append a preview class.

[Java]
Public class CameraActivity extends Activity {
Private Camera mCamera;
Private CameraPreview mPreview;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
// Create an instance of 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 );
}
}
Public class CameraActivity extends Activity {
Private Camera mCamera;
Private CameraPreview mPreview;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

// Create an instance of 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 this example references the Same Name method in the "use camera" section.

Author: nkmnkm
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.