欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android调用手机拍照功能的方法

程序员文章站 2023-11-29 22:06:22
本文实例讲述了android调用手机拍照功能的方法。分享给大家供大家参考。具体如下: 一、main.xml布局文件:

本文实例讲述了android调用手机拍照功能的方法。分享给大家供大家参考。具体如下:

一、main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <imageview android:id="@+id/imageview"
  android:adjustviewbounds="true"
  android:layout_gravity="center"
  android:minwidth="150dip"
  android:minheight="150dip"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <button android:id="@+id/btnphone"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="相册" />
 <button android:id="@+id/btntakepicture"
  android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  android:text="拍照" />
</linearlayout>

二、核心代码:

package com.ljq.test;
import java.io.bytearrayoutputstream;
import java.io.file;
import android.app.activity;
import android.content.intent;
import android.graphics.bitmap;
import android.net.uri;
import android.os.bundle;
import android.os.environment;
import android.provider.mediastore;
import android.view.view;
import android.widget.button;
import android.widget.imageview;
public class testactivity extends activity {
 private static final int none = 0;
 private static final int photo_graph = 1;// 拍照
 private static final int photo_zoom = 2; // 缩放
 private static final int photo_resoult = 3;// 结果
 private static final string image_unspecified = "image/*";
 private imageview imageview = null;
 private button btnphone = null;
 private button btntakepicture = null;
 @override
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.main);
  imageview = (imageview) findviewbyid(r.id.imageview);
  btnphone = (button) findviewbyid(r.id.btnphone);
  btnphone.setonclicklistener(onclicklistener);
  btntakepicture = (button) findviewbyid(r.id.btntakepicture);
  btntakepicture.setonclicklistener(onclicklistener);
 }
 private final view.onclicklistener onclicklistener = new view.onclicklistener() {
  @override
  public void onclick(view v) {
   if(v==btnphone){ //从相册获取图片
    intent intent = new intent(intent.action_pick, null);
    intent.setdataandtype(mediastore.images.media.external_content_uri, image_unspecified);
    startactivityforresult(intent, photo_zoom);
   }else if(v==btntakepicture){ //从拍照获取图片
    intent intent = new intent(mediastore.action_image_capture);
    intent.putextra(mediastore.extra_output, uri.fromfile(new file(environment
     .getexternalstoragedirectory(),"temp.jpg")));
    startactivityforresult(intent, photo_graph);
   }
  }
 };
 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
  if (resultcode == none)
   return;
  // 拍照
  if (requestcode == photo_graph) {
   // 设置文件保存路径
   file picture = new file(environment.getexternalstoragedirectory()
     + "/temp.jpg");
   startphotozoom(uri.fromfile(picture));
  }
  if (data == null)
   return;
  // 读取相册缩放图片
  if (requestcode == photo_zoom) {
   startphotozoom(data.getdata());
  }
  // 处理结果
  if (requestcode == photo_resoult) {
   bundle extras = data.getextras();
   if (extras != null) {
    bitmap photo = extras.getparcelable("data");
    bytearrayoutputstream stream = new bytearrayoutputstream();
    photo.compress(bitmap.compressformat.jpeg, 75, stream);// (0-100)压缩文件
    //此处可以把bitmap保存到sd卡中
    imageview.setimagebitmap(photo); //把图片显示在imageview控件上
   }
  }
  super.onactivityresult(requestcode, resultcode, data);
 }
 /**
  * 收缩图片
  * 
  * @param uri
  */
 public void startphotozoom(uri uri) {
  intent intent = new intent("com.android.camera.action.crop");
  intent.setdataandtype(uri, image_unspecified);
  intent.putextra("crop", "true");
  // aspectx aspecty 是宽高的比例
  intent.putextra("aspectx", 1);
  intent.putextra("aspecty", 1);
  // outputx outputy 是裁剪图片宽高
  intent.putextra("outputx", 300);
  intent.putextra("outputy", 500);
  intent.putextra("return-data", true);
  startactivityforresult(intent, photo_resoult);
 }
}

希望本文所述对大家的android程序设计有所帮助。