Android开发培训(07)--相机多媒体

来源:互联网 发布:h3c防火墙web配置 端口 编辑:程序博客网 时间:2024/04/25 11:50

建立多媒体程序

这篇文章教你如何建立内容丰富的多媒体程序。

第一节 简单地拍照

假设你实现了个天气监测功能的程序,你的设备对的app会不定期去拍摄照片。拍照片只是你的程序的小部分,你想使用最短的用时拍照,不用重新启动相机。很高兴的是,绝大部分的Android手机都至少会有一个相机应用安装在上面。这篇文章中,你将会学习怎么用它来拍照。

1. 申请相机权限

如果你的程序必须拍照,那么设备必须有一个相机,为了表示你的spp需要有一个相机,你应该在你的manifest文件中放置一个<uses-feature>属性

<manifest ... >    <uses-feature android:name="android.hardware.camera"                  android:required="true" />    ...</manifest>
如果你的程序需要使用,但是不需要相机来完成相应的功能,可以将android:required设置为false.这样的话,google play允许你的下载这个应用,即使这个设备没有相机。然后你应该自己在运行程序的时候去检测相机功能能不能用,通过调用hasSystemFeature(PackageManager.FEATURE_CAMERA)方法实现。如果相机不能用,那你应该关掉你的相机功能。

2. 使用相机程序拍照

android系统运行你通过调用intent方式去调用相机,这个intent会去启动另一个activity,然后返回图片数据的handle值给调用的程序。

下面是通过intent去拍照的代码

static final int REQUEST_IMAGE_CAPTURE = 1;private void dispatchTakePictureIntent() {    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);    }}
记着resolveActivity函数很重要,因为它会返回第一个会处理这个intent的activity,如果检查没有相应的activity的话,这样程序也不会崩溃。

3. 获取缩略图片

你可能需要从拍照的程序获取返回的相片,然后利用它做点其它的事情。

Android系统的程序已经封装好了返回的intent给onActivityResult,它是作为一个很小的Bitmap传递的,下面的代码将获取这张图片,然后在ImageView中显示。

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {        Bundle extras = data.getExtras();        Bitmap imageBitmap = (Bitmap) extras.get("data");        mImageView.setImageBitmap(imageBitmap);    }}
记住从“data"来的缩略图最多只适合一个小图标,除了这个没有其它的用处。

4. 保存全尺寸图片

记住相机应用可以保存整个文件,如果你给文件的路径。你必须给相机应用提供全路径才能保存。

一般来说,所有的使用相机app拍照的图片都应该保存在公共的外部存储空间,因为他们可以被所有的应用使用。可以通过getExternalStoragePublicDirectory()传递DIRECTORY_PICTURES值函数获取。因为这个目录所有的程序都可以访问,读写这个目录需要READ_EXTERNAL_STORAGE andWRITE_EXTERNAL_STORAGE权限,写权限已经包括了读权限,所以你可以值需要申请一个写权限。

<manifest ...>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    ...</manifest>
通过getExternalFileDir()和getFileDir()获取的文件路径都会在程序卸载的时候被删除。

一旦你写好了文件的路径,你需要创建一个没有冲突的文件名称。你可能也会把路径保留在一个变量中供后面的代码使用。这个是一个使用时间戳作为文件的唯一名字的方法。

String mCurrentPhotoPath;private File createImageFile() throws IOException {    // Create an image file name    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());    String imageFileName = "JPEG_" + timeStamp + "_";    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);    File image = File.createTempFile(        imageFileName,  /* prefix */        ".jpg",         /* suffix */        storageDir      /* directory */    );    // Save a file: path for use with ACTION_VIEW intents    mCurrentPhotoPath = image.getAbsolutePath();    return image;}
static final int REQUEST_TAKE_PHOTO = 1;private void dispatchTakePictureIntent() {    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    // Ensure that there's a camera activity to handle the intent    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {        // Create the File where the photo should go        File photoFile = null;        try {            photoFile = createImageFile();        } catch (IOException ex) {            // Error occurred while creating the File            ...        }        // Continue only if the File was successfully created        if (photoFile != null) {            Uri photoURI = FileProvider.getUriForFile(this,                                                  "com.example.android.fileprovider",                                                  photoFile);            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);        }    }}
注意我们使用getUriFile(Context, String, File)会返回一个content:// uri,对于7.0的版本,翻过包空间传递一个file:// uri会引起一个FileUriExposeException.

因此我们,使用一个更通用的FileProvider方法存储照片。

现在,我们需要配置FileProvider,在你的app manifest文件中,加入你的provider

<application>   ...   <provider        android:name="android.support.v4.content.FileProvider"        android:authorities="com.example.android.fileprovider"        android:exported="false"        android:grantUriPermissions="true">        <meta-data            android:name="android.support.FILE_PROVIDER_PATHS"            android:resource="@xml/file_paths"></meta-data>    </provider>    ...</application>
却本授权的字符串和getUriFile第二个字符串是相同的,在meta-data中,提供了定义,那么期望的路径在res/xml/file_paths.xml中可以找到。

这里是这个配置文件的内容

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">    <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" /></paths>
这个路径和通过getExternFileDir(Environment.DIRECTORY_PICTURES)对应,确保com.example.package.name和你真正的包名对应。同事,也应该检查FileProvider的文件路径。

5.  j将图片加入到图库中

当你通过intent创建一个图片之后,你应该知道你的图片存储在哪里。因为你是可以首先指定它放在哪里的。对于其它的任何人,然其它任何能够访问你的图片最简单的做法就是让你的系统Media Provider能过获取图片。

注意如果你使用getExternalFileDir()获取的路径保存的图片,你的media Scanner是没有办法获取这个图片的,因为图片是你的app私有的。

下面的例子展示了如果调用系统的media Scanner加入你的图片到media Provider的数据库中,使它能被其它的android的gallery程序和其它程序访问。

private void galleryAddPic() {    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);    File f = new File(mCurrentPhotoPath);    Uri contentUri = Uri.fromFile(f);    mediaScanIntent.setData(contentUri);    this.sendBroadcast(mediaScanIntent);}
6. 缩放图片

对于有限的资源来说,全尺寸的图片可能太大,如果你发现你的app已经快要占用完内存之后,你可以通过将jpeg文件缩放当一个合适的大小来减少对内存的占用。下面这个例子就展现了这项技术。

private void setPic() {    // Get the dimensions of the View    int targetW = mImageView.getWidth();    int targetH = mImageView.getHeight();    // Get the dimensions of the bitmap    BitmapFactory.Options bmOptions = new BitmapFactory.Options();    bmOptions.inJustDecodeBounds = true;    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);    int photoW = bmOptions.outWidth;    int photoH = bmOptions.outHeight;    // Determine how much to scale down the image    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);    // Decode the image file into a Bitmap sized to fill the View    bmOptions.inJustDecodeBounds = false;    bmOptions.inSampleSize = scaleFactor;    bmOptions.inPurgeable = true;    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);    mImageView.setImageBitmap(bitmap);}

下面是根据官网上提供的sample修改的自己的sample,因为官网那个比较版本很就,所以只能拿来参考,

但是在做的过程中最后一步,添加galleryAddPic()函数的时候还是有问题,gallery中还是没有办法扫描到图片,可能这是一个私有目录,gallery就不会添加。其余的都已经完成,包括前面一章讲到的通过fileprovider提供共享目录,下面是关键的代码

Manifest.xml配置文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.www.multimedia">    <uses-feature android:name="android.hardware.camera2" android:required="true" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>        <provider            android:authorities="com.example.www.multimedia.fileprovider"            android:name="android.support.v4.content.FileProvider"            android:exported="false"            android:grantUriPermissions="true">            <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_paths">            </meta-data>        </provider>    </application></manifest>
其中在xml目录下面设置了file_paths目录

file_paths.xml指定了文件分享的目录

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">    <external-path name="files" path="Android/data/com.example.www.multimedia"/>    </paths>

其中com.example.www.multimedia为文件的包名,相机应用拍好的照片会保存在files/Pictures文件目录下面

下面是main_activity的布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:android="http://schemas.android.com/apk/res/android">    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/btnIntend"            android:layout_width="0dp"            android:layout_weight="1"            android:text="@string/btnIntend"            android:layout_height="wrap_content"/>        <Button            android:id="@+id/btnIntendS"            android:layout_width="0dp"            android:layout_weight="1"            android:text="@string/btnIntendS"            android:layout_height="wrap_content"/>    </LinearLayout>    <ImageView        android:id="@+id/imageView1"        android:layout_width="300dp"        android:layout_height="500dp"        android:visibility="visible"/></LinearLayout>
其中定义了两个按钮,拍大图和拍小图,小图直接通过intent的data传输和保存,大图则是通过传递文件的uri,指定文件保存的路径保存。

package com.example.www.multimedia;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.support.v4.content.FileProvider;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;public class MainActivity extends Activity {    private final String TAG = "MultiMedia";    private static final int ACTION_TAKE_PHOTO_B = 1;    private static final int ACTION_TAKE_PHOTO_S = 2;    static final int REQUEST_TAKE_PHOTO = 3;    Button picBtn;    Button picSBtn;    private ImageView mImageView;    String mCurrentPhotoPath;    Button.OnClickListener mTakePicSOnClickListener = new Button.OnClickListener() {        @Override        public void onClick(View view) {            dispatchTakePictureIntent(ACTION_TAKE_PHOTO_S);        }    };    Button.OnClickListener mTakePicOnClickListener = new Button.OnClickListener() {        @Override        public void onClick(View view) {            dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mImageView = (ImageView) findViewById(R.id.imageView1);        picBtn = (Button) findViewById(R.id.btnIntend);        picSBtn = (Button) findViewById(R.id.btnIntendS);        setBtnListenerOrDisable(picBtn, mTakePicOnClickListener, MediaStore.ACTION_IMAGE_CAPTURE);        setBtnListenerOrDisable(picSBtn, mTakePicSOnClickListener, MediaStore.ACTION_IMAGE_CAPTURE);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (requestCode) {            case ACTION_TAKE_PHOTO_B: {                if (resultCode == RESULT_OK) {                    handleBigCameraPhoto();                }                break;            }            case ACTION_TAKE_PHOTO_S: {                if (resultCode == RESULT_OK) {                    handleSmallCameraPhoto(data);                }                break;            }        }    }    private void dispatchTakePictureIntent(int actionCode) {        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        switch(actionCode) {            case ACTION_TAKE_PHOTO_B: {                // Ensure that there's a camera activity to handle the intent                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {                    // Create the File where hte photo should go                    File photoFile = null;                    try {                        photoFile = createImageFile(this);                    } catch (IOException e) {                        e.printStackTrace();                    }                    Log.e(TAG, "wangrl");                    // Continue only if the File was successfully created                    if (photoFile != null) {                        Uri photoURI = FileProvider.getUriForFile(this,                                "com.example.www.multimedia", photoFile);                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);                        startActivityForResult(takePictureIntent, ACTION_TAKE_PHOTO_B);                    }                }            }                break;            case ACTION_TAKE_PHOTO_S: {                startActivityForResult(takePictureIntent, actionCode);                break;            }            default:                break;        }    }    private void handleSmallCameraPhoto(Intent intent) {        Bundle extras = intent.getExtras();        Bitmap mImageBitmap = (Bitmap) extras.get("data");        mImageView.setImageBitmap(mImageBitmap);        mImageView.setVisibility(View.VISIBLE);    }    private void handleBigCameraPhoto() {        setPic();        galleryAddPic();    }    private void galleryAddPic() {        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);        File f = new File(mCurrentPhotoPath);        Uri contentUri = Uri.fromFile(f);        mediaScanIntent.setData(contentUri);        this.sendBroadcast(mediaScanIntent);    }    private void setPic() {        // Get the dimensions of the View        int targetW = mImageView.getWidth();        int targetH = mImageView.getHeight();        // Get the demensions of the bitmap        BitmapFactory.Options bmOptions = new BitmapFactory.Options();        bmOptions.inJustDecodeBounds = true;        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);        int photoW = bmOptions.outWidth;        int photoH = bmOptions.outHeight;        // Detetmine how much to scale down the image        int scaleFator = Math.min(photoW/targetW, photoH/targetH);        // Decode the image file int a Bitmap size to fill the view        bmOptions.inJustDecodeBounds = false;        bmOptions.inSampleSize = scaleFator;        bmOptions.inPurgeable = true;        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);        mImageView.setImageBitmap(bitmap);    }    private File createImageFile(MainActivity mainActivity) throws IOException {        // Create an image file name        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());        String imageFileName = "JPEG_" + timeStamp + "_";        File storageDir = mainActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);        File image = File.createTempFile(                imageFileName,      /* prefix */                ".jpg",             /* suffix */                storageDir          /* directory */        );        // Save a file: path for use with ACTION_VIEW intents        mCurrentPhotoPath = image.getAbsolutePath();        return image;    }    public static  boolean isIntentAvailable(Context context, String action) {        final PackageManager packageManager = context.getPackageManager();        final Intent intent = new Intent(action);        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);        return list.size() > 0;    }    private void setBtnListenerOrDisable(Button btn, Button.OnClickListener onClickListener, String intentName) {        if (isIntentAvailable(this, intentName)) {            btn.setOnClickListener(onClickListener);        } else {            btn.setText(getText(R.string.cannot).toString() + " " + btn.getText());            btn.setClickable(false);        }    }}

第二小节 记录视频

这小节教你怎么使用相机app进行拍摄视频。

你的app需要记录视频,这里是你需要做的一小部分,很高兴的收,你的设备提供一个相机应用可以记录视频,这篇文章就是教你怎么做的。

1. 获取相机权限

需要在manifest文件中使用<uses-feature>

<manifest ... >    <uses-feature android:name="android.hardware.camera"                  android:required="true" />    ...</manifest>
还是和拍照是一样的。

2. 使用相机app记录视频

还是使用的是intent调用相机应用,首先创建一个intent,然后调用一个activity,最后是一些处理视频的代码。

这是怎么调用视频的代码

static final int REQUEST_VIDEO_CAPTURE = 1;private void dispatchTakeVideoIntent() {    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);    if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {        startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);    }}
3. 查看视频

相机应用会返回一个指向video的位置的uri给应用,应用可以通过调用这个uri,获取播放的视频,

代码和上面的代码大部分是重复的,这里只是贴出和video相关的代码,需要mVideo.start()一下视频才会播放,和指南上讲的有点不同。

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (requestCode) {            case ACTION_TAKE_PHOTO_B: {                if (resultCode == RESULT_OK) {                    handleBigCameraPhoto();                }                break;            }            case ACTION_TAKE_PHOTO_S: {                if (resultCode == RESULT_OK) {                    handleSmallCameraPhoto(data);                }                break;            }            case REQUEST_VIDEO_CAPTURE: {                if (resultCode == RESULT_OK) {                    Log.e(TAG, "wangrll");                    Uri videoUri = data.getData();                    mVideoView.setVideoURI(videoUri);                    mVideoView.start();                    mVideoView.setVisibility(View.VISIBLE);                }            }        }    }    private void dispatchTakePictureIntent(int actionCode) {        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        switch(actionCode) {            case ACTION_TAKE_PHOTO_B: {                // Ensure that there's a camera activity to handle the intent                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {                    // Create the File where hte photo should go                    File photoFile = null;                    try {                        photoFile = createImageFile(this);                    } catch (IOException e) {                        e.printStackTrace();                    }                    // Continue only if the File was successfully created                    if (photoFile != null) {                        Uri photoURI = FileProvider.getUriForFile(this,                                "com.example.www.multimedia", photoFile);                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);                        startActivityForResult(takePictureIntent, ACTION_TAKE_PHOTO_B);                    }                }            }                break;            case ACTION_TAKE_PHOTO_S: {                startActivityForResult(takePictureIntent, actionCode);                break;            }            default:                break;        }    }    private  void dispatchTakeVideoIntent() {        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);        if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);        }    }

第三节 控制相机进行拍照

这个小节讲述直接通过api控制相机硬件,

直接编写相机的拍照的代码比通过请求系统的相机app进行拍照写的代码要多很多。如果你向建立一个直接在你的app中使用相机的程序,那么请看下面的章节。

1. 打开一个相机对象

获取一个相机对象,是控制相机的第一步。像android系统相机应用那么样,一般是通过在oncreate()新开一个线程打开相机,这是一个好主意,因为这不会阻塞你的ui主线程。在一个更基本的实现中,打开相机可以推迟到onResume方法中执行,这样可以使得控制流程更简单有效。

通过调用Camera.open()方法打开相机,如果相机被其它的程序打开就会抛出一个异常

private boolean safeCameraOpen(int id) {    boolean qOpened = false;    try {        releaseCameraAndPreview();        mCamera = Camera.open(id);        qOpened = (mCamera != null);    } catch (Exception e) {        Log.e(getString(R.string.app_name), "failed to open Camera");        e.printStackTrace();    }    return qOpened;}private void releaseCameraAndPreview() {    mPreview.setCamera(null);    if (mCamera != null) {        mCamera.release();        mCamera = null;    }}
从sdk 9开始,手机支持不止一个相机,如果你通过open()调用没有传递参数,那么会获得一个后置相机。

2. 创建一个相机预览

拍照通常用户都会预览,因此你需要创建一个SurfaceView能够抓取预览相机传感器拍的内容。

预览类

你需要一个预览类,这个类需要实现android.view.SurfaceHolder.Callback接口,这个类的作用是将相机硬件的数据传递到你的程序之中。

class Preview extends ViewGroup implements SurfaceHolder.Callback {    SurfaceView mSurfaceView;    SurfaceHolder mHolder;    Preview(Context context) {        super(context);        mSurfaceView = new SurfaceView(context);        addView(mSurfaceView);        // Install a SurfaceHolder.Callback so we get notified when the        // underlying surface is created and destroyed.        mHolder = mSurfaceView.getHolder();        mHolder.addCallback(this);        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    }...}

这个预览必须传递一个Camera对象,在开启预览之前。

3. 设置和开启预览

一个相机实例和它相关的预览必须按指定的顺序创建,首先应该创建相机实例。在下面的代码片段中,初始化相机放在被封装在setCamera()方法中,不论用户做了什么操作改变相机,preview都会在surfaceChanged()回调方法中重新启动。

public void setCamera(Camera camera) {    if (mCamera == camera) { return; }    stopPreviewAndFreeCamera();    mCamera = camera;    if (mCamera != null) {        List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();        mSupportedPreviewSizes = localSizes;        requestLayout();        try {            mCamera.setPreviewDisplay(mHolder);        } catch (IOException e) {            e.printStackTrace();        }        // Important: Call startPreview() to start updating the preview        // surface. Preview must be started before you can take a picture.        mCamera.startPreview();    }}

4. 更改相机设置

相机设置会改变相机拍照的形式,比如区域的等级,比如曝光,这个例子只是改变预览的大小,可以查看Camera应用的源码获取更多信息。

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {    // Now that the size is known, set up the camera parameters and begin    // the preview.    Camera.Parameters parameters = mCamera.getParameters();    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);    requestLayout();    mCamera.setParameters(parameters);    // Important: Call startPreview() to start updating the preview surface.    // Preview must be started before you can take a picture.    mCamera.startPreview();}
5. 设置预览的方向

很多应用会默认使用横屏拍摄,因为这是相机传感器的的排列方式,这个设置并不影响你拍竖屏的照片,因为设备的方向记录在EXIF头文件中。

通过setCameraDisplayOrientation()方法可以改变预览的方向,而不影响相机的记录。

6. 使用Camera.takePicture()方法进行拍照,你可以创建Camera.PictureCallback()和Camera.ShutterCallback()对象,将他们传入Camera.takePicture()方法中。

如果你向连续的抓取图片,你可以创建一个Camera.PreviewCallback对象,这个对象实现onPreviewFrame()方法。

7. 重新预览

在你拍完照片之后你必须重新启动你的预览,在用户能够进行拍下一张图片之前。在这个例子中,通过重写shutter button来重新预览。

@Overridepublic void onClick(View v) {    switch(mPreviewState) {    case K_STATE_FROZEN:        mCamera.startPreview();        mPreviewState = K_STATE_PREVIEW;        break;    default:        mCamera.takePicture( null, rawCallback, null);        mPreviewState = K_STATE_BUSY;    } // switch    shutterBtnConfig();}
8. 停止预览和释放相机

一旦你的app使用完成相机,就需要释放资源。特别的你需要释放Camera对象,如果不释放你很有可能让别的程序崩溃。

在不再预览的时候释放相机是最好的时候

下面这个是一个示例

public void surfaceDestroyed(SurfaceHolder holder) {    // Surface will be destroyed when we return, so stop the preview.    if (mCamera != null) {        // Call stopPreview() to stop updating the preview surface.        mCamera.stopPreview();    }}/** * When this function returns, mCamera will be null. */private void stopPreviewAndFreeCamera() {    if (mCamera != null) {        // Call stopPreview() to stop updating the preview surface.        mCamera.stopPreview();        // Important: Call release() to release the camera for use by other        // applications. Applications should release the camera immediately        // during onPause() and re-open() it during onResume()).        mCamera.release();        mCamera = null;    }}

第二章 打印内容

第一节 打印照片

第二节 打印html文档

第三节 打印常规文件

原创粉丝点击