实现调用Android手机的拍照功能


很简单,直接上示例吧

1 xml

 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     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <Button
12         android:id="@+id/btnTakePic"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="拍照" />
16 
17     <ImageView
18         android:id="@+id/imgview"
19         android:layout_width="100dp"
20         android:layout_height="100dp"
21         android:layout_alignLeft="@+id/btnTakePic"
22         android:layout_below="@+id/btnTakePic"
23         android:layout_marginTop="38dp" />
24 
25 </RelativeLayout>

2 java代码

  1 package com.example.takepictrures;
  2 
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.File;
  5 import java.io.IOException;
  6 import java.text.SimpleDateFormat;
  7 import java.util.Date;
  8 import com.example.util.saveToSdCard;
  9 import android.net.Uri;
 10 import android.os.Bundle;
 11 import android.os.Environment;
 12 import android.provider.MediaStore;
 13 import android.provider.MediaStore.Audio.Media;
 14 import android.app.Activity;
 15 import android.content.ContentValues;
 16 import android.content.Intent;
 17 import android.graphics.Bitmap;
 18 import android.graphics.BitmapFactory;
 19 import android.view.Menu;
 20 import android.view.View;
 21 import android.view.View.OnClickListener;
 22 import android.widget.Button;
 23 import android.widget.ImageView;
 24 
 25 public class MainActivity extends Activity implements OnClickListener {
 26 
 27     public Button button;
 28     private static final int PHOTO_GRAPH = 1;// ����
 29     public ImageView imageView;
 30 
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_main);
 35         imageView = (ImageView) this.findViewById(R.id.imgview);
 36         button = (Button) this.findViewById(R.id.btnTakePic);
 37         button.setOnClickListener(this);
 38 
 39     }
 40 
 41     @Override
 42     public boolean onCreateOptionsMenu(Menu menu) {
 43         // Inflate the menu; this adds items to the action bar if it is present.
 44         getMenuInflater().inflate(R.menu.main, menu);
 45         return true;
 46     }
 47 
 48     @Override
 49     public void onClick(View view) {
 50         switch (view.getId()) {
 51         case R.id.btnTakePic:
 52             Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 53             startActivityForResult(intent, PHOTO_GRAPH);
 54             break;
 55 
 56         default:
 57             break;
 58         }
 59     }
 60 
 61     @Override
 62     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 63         if (resultCode == NONE)
 64             return;
 65 
 66         if (requestCode == PHOTO_GRAPH) {
 67 
 68             File picture = new File(Environment.getExternalStorageDirectory()
 69                     + "/temp.jpg");
 70             // startPhotoZoom(Uri.fromFile(picture));
 71         }
 72 
 73         if (data == null)
 74             return;
 75 
 76         // if (requestCode == PHOTO_ZOOM) {
 77         // startPhotoZoom(data.getData());
 78         // }
 79         
 80         String fileName = "";
 81         if (requestCode == 1) {
 82             Bundle extras = data.getExtras();
 83             if (extras != null) {
 84                 Bitmap photo = extras.getParcelable("data");
 85                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 86                 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)ѹ���ļ�
 87                  
///这儿保存到sd卡就Ok了 88 try { 89 fileName = saveToSdCard.getFileName(); 90 saveToSdCard.saveMyBitmap(photo, fileName); 91 } catch (IOException e) { 92 // TODO Auto-generated catch block 93 e.printStackTrace(); 94 } 95 imageView.setImageBitmap(photo); 96 } 97 } 98 super.onActivityResult(requestCode, resultCode, data); 99 } 100 101 102 // public void startPhotoZoom(Uri uri) { 103 // Intent intent = new Intent("com.android.camera.action.CROP"); 104 // intent.setDataAndType(uri, IMAGE_UNSPECIFIED); 105 // intent.putExtra("crop", "true"); 106 // // aspectX aspectY 107 // intent.putExtra("aspectX", 1); 108 // intent.putExtra("aspectY", 1); 109 // // outputX outputY 110 // intent.putExtra("outputX", 300); 111 // intent.putExtra("outputY", 500); 112 // intent.putExtra("return-data", true); 113 // startActivityForResult(intent, PHOTO_RESOULT); 114 // } 115 116 }

 

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告