当前位置:操作系统 > 安卓/Android >>

Android Camera系列开发 (一): 通过Intent拍照

概述

使用Camera有两种方式:通过Intent使用已有的app和通过Camera构建自己的app。

Camera相关声明

如果你的应用程序要使用Camera,则必须获得使用许可,需要在AndroidManifest.xml中加入如下声明。

<uses-permissionandroid:name="android.permission.CAMERA" />

如果你的应用程序必须有Camera才能使用,则声明如下:

<uses-featureandroid:name="android.hardware.camera" />

否则应声明如下:

<uses-featureandroid:name="android.hardware.camera"android:required="false" />

其他相关声明:

存储:<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />

语音:<uses-permissionandroid:name="android.permission.RECORD_AUDIO" />

位置:<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />

 

核心类

Itent:通过它进行Camera编程更简单。

Camera:通过它可以控制相机,构建功能更强大的Camera应用。

Su易做图ceView:提供实时预览功能。

 

通过Itent来实现拍照

第一步:在Eclipse中创建一个名为Photo的Android工程,可参见Hello world的例子;

第二步:在AndroidManifest.xml中添加使用Camera相关的声明如下:

<uses-permissionandroid:name="android.permission.CAMERA"/>

第三步:编写MainActivity类。

(1) 添加成员变量

(2) 修改OnCreate方法

(3) 增加onActivityResult方法

(4) 新增两个辅助方法

类的源代码如下:

[java]
public class MainActivity extends Activity { 
    private static final int MEDIA_TYPE_IMAGE = 1; 
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; 
     
    private Intent intent  = null; 
    private Uri fileUri    = null; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//create a intent to take picture  
        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); 
    } 
 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, 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  
            } 
        } 
    } 
     
    /** Create a file Uri for saving an image or video */ 
    private static Uri getOutputMediaFileUri(int type){ 
          return Uri.fromFile(getOutputMediaFile(type)); 
    } 
 
    /** Create a File for saving an image or video */ 
    private static File getOutputMediaFile(int type){ 
        // To be safe, you should check that the SDCard is mounted  
        // using Environment.getExternalStorageState() before doing this.  
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( 
                  Environment.DIRECTORY_PICTURES), "MyCameraApp"); 
        // This location works best if you want the created images to be shared  
        // between applications and persist after your app has been uninstalled.  
 
        if (! mediaStorageDir.exists()){ 
            if (! mediaStorageDir.mkdirs()){ 
                Log.d("MyCameraApp", "failed to create directory"); 
                return null; 
            } 
        } 
 
        // Create a media file name  
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
        File mediaFile = null; 
        if (type == MEDIA_TYPE_IMAGE){ 
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
            "IMG_"+ timeStamp + ".jpg"); 
        } 
 
        return mediaFile; 
    } 

public class MainActivity extends Activity {
 private static final int MEDIA_TYPE_IMAGE = 1;
 private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,