码迷,mamicode.com
首页 > 移动开发 > 详细

Android系统拍照源码

时间:2016-11-05 12:04:11      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:image   name   tput   利用   block   extern   extra   catch   graphics   

个人对于Android系统拍照的一些总结:一种自定义图片拍照路径 ,另一种直接利用Android拍照后经过处理的缩略图

特别注意第一种方式需要增加SDK读写权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 下面直接上源码

 1 package com.example.camerademo;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 
 8 import android.app.Activity;
 9 import android.content.Intent;
10 import android.graphics.Bitmap;
11 import android.graphics.BitmapFactory;
12 import android.net.Uri;
13 import android.os.Bundle;
14 import android.os.Environment;
15 import android.provider.MediaStore;
16 import android.view.View;
17 import android.widget.ImageView;
18 
19 public class MainActivity extends Activity {
20     
21     public static final int REQUEST_CAMERA_CODE = 100;
22     public static final int REQUEST_CAMERA_AUTO = 101;
23     ImageView mIv;
24     String path;
25     
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.activity_main);
30         mIv = (ImageView) findViewById(R.id.iv);
31         path =  Environment.getExternalStorageDirectory() + "/" + "temp.jpg";
32     }
33     
34     public void doClick1(View v) {
35         Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
36         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
37         startActivityForResult(intent, REQUEST_CAMERA_CODE);
38     }
39     public void doClick2(View v) {
40         Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
41         startActivityForResult(intent, REQUEST_CAMERA_AUTO);
42     }
43     
44     
45     
46     @Override
47     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
48         super.onActivityResult(requestCode, resultCode, data);
49         if(resultCode == RESULT_OK) {
50             if(requestCode == REQUEST_CAMERA_CODE ) {
51                 FileInputStream fis = null;
52                 try {
53                     fis = new FileInputStream(path);
54                     Bitmap bitmap = BitmapFactory.decodeStream(fis);
55                     if(bitmap != null) {
56                         mIv.setImageBitmap(bitmap);
57                     }
58                 } catch (FileNotFoundException e) {
59                     // TODO Auto-generated catch block
60                     e.printStackTrace();
61                 } finally{
62                     if(fis != null) {
63                         try {
64                             fis.close();
65                         } catch (IOException e) {
66                             // TODO Auto-generated catch block
67                             e.printStackTrace();
68                         }
69                     }
70                 }
71                 
72             } else if(requestCode == REQUEST_CAMERA_AUTO) {
73                 Bundle bundle = data.getExtras();
74                 Bitmap bitmap = (Bitmap) bundle.get("data");
75                 if(bitmap != null) {
76                     mIv.setImageBitmap(bitmap);
77                 }
78             }
79         }
80         
81     }
82     
83 }
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="${relativePackage}.${activityClass}" >
 6 
 7     <TextView
 8         android:id="@+id/textView1"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="@string/hello_world" />
12 
13     <Button
14         android:id="@+id/startCamera"
15         android:onClick="doClick1"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_below="@+id/textView1"
19         android:layout_centerHorizontal="true"
20         android:layout_marginTop="17dp"
21         android:text="startCamera_custome_address" />
22 
23     <ImageView
24         android:id="@+id/iv"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:layout_alignLeft="@+id/startCamera"
28         android:layout_centerVertical="true"
29         android:layout_marginLeft="32dp" />
30 
31     <Button
32         android:id="@+id/button1"
33         android:onClick="doClick2"
34         android:layout_width="wrap_content"
35         android:layout_height="wrap_content"
36         android:layout_alignLeft="@+id/startCamera"
37         android:layout_below="@+id/startCamera"
38         android:layout_marginTop="41dp"
39         android:text="start_camera_auto_addres" />
40 
41 </RelativeLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.camerademo"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="21" />
10     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
11 
12     <application
13         android:allowBackup="true"
14         android:icon="@drawable/ic_launcher"
15         android:label="@string/app_name"
16         android:theme="@style/AppTheme" >
17         <activity
18             android:name=".MainActivity"
19             android:label="@string/app_name" >
20             <intent-filter>
21                 <action android:name="android.intent.action.MAIN" />
22 
23                 <category android:name="android.intent.category.LAUNCHER" />
24             </intent-filter>
25         </activity>
26     </application>
27 
28 </manifest>

 

  

Android系统拍照源码

标签:image   name   tput   利用   block   extern   extra   catch   graphics   

原文地址:http://www.cnblogs.com/androidsihai/p/6032642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!