• 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>

      

  • 相关阅读:
    深入理解JavaScript中的this关键字
    使用yii的layout,加入<?php echo $content; ?>这句话时,它会自动在子页面上面添加一个div包裹
    使用 BEGINCONTENT() 和 ENDCONTENT() 设定 YII 的 LAYOUTS
    孙悟空的师傅是谁
    PHP MemCached高级缓存配置图文教程
    C++中的基类与派生类
    矩阵快速幂
    华为上机测试题
    2016网易研发题目
    冒泡排序
  • 原文地址:https://www.cnblogs.com/androidsihai/p/6032642.html
Copyright © 2020-2023  润新知