Android系统拍照源码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android系统拍照源码相关的知识,希望对你有一定的参考价值。

个人对于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系统拍照源码的主要内容,如果未能解决你的问题,请参考以下文章

基于Android的远程视频监控系统(含源码)

Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段

从片段中的相机拍照

调用 android 系统拍照结合 android-crop 裁剪图片

Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段

拍照后返回fragment