700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android 调用手机系统照相机拍照

Android 调用手机系统照相机拍照

时间:2022-02-28 04:23:09

相关推荐

Android 调用手机系统照相机拍照

Android 调用手机系统照相机拍照 并用ImageView显示 此方法获得的是拍照的原图片而非缩略图。

1.首先是布局文件activity_main.xml

<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/iv"android:layout_width="200dp"android:layout_height="200dp"android:background="@null"android:contentDescription="@string/app_name" android:layout_centerInParent="true"/><Buttonandroid:id="@+id/update_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/iv"android:layout_centerInParent="true"android:layout_marginTop="10dp"android:text="拍照" /></RelativeLayout>

2.接着是MainActivity

package com.updateimage;import java.io.File;import java.io.IOException;import java.util.concurrent.ExecutionException;import .Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class MainActivity extends Activity {private Button btn;private ImageView iv;private File file;private Bitmap photo;private static final int cameraCode = 0;public static int photoW, photoH;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn = (Button) findViewById(R.id.update_btn);iv = (ImageView) findViewById(R.id.iv);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {takePictures(1);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);Log.d("TAG4", "--requestCode--:" + requestCode + "--resultCode--:"+ resultCode);if (requestCode == cameraCode) {if (file != null && file.exists()) {Log.d("TAG4", "-----照片地址:-----=" + file.getPath());photo = zoomPhoto(file.getPath());iv.setImageBitmap(photo);}}}/*** 拍照*/private void takePictures(int n) {// 释放Bitmap对象 防止内存溢出destoryBitmap();String state = Environment.getExternalStorageState();if (state.equals(Environment.MEDIA_MOUNTED)) {String saveDir = Environment.getExternalStorageDirectory()+ "/temple";File dir = new File(saveDir);if (!dir.exists()) {dir.mkdir();}file = new File(saveDir, "temp" + n + ".jpg");file.delete();if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();Toast.makeText(MainActivity.this, "保存地址为空!",Toast.LENGTH_LONG).show();return;}}Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));startActivityForResult(intent, cameraCode);} else {Toast.makeText(MainActivity.this, "请插入SD卡", Toast.LENGTH_LONG).show();}}/*** 缩放图片(显示)* * @param bitmap* @return*/private Bitmap zoomPhoto(String path) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;// 获取这个图片的宽和高Bitmap bitmap = BitmapFactory.decodeFile(path, options);photoW = options.outWidth;photoH = options.outHeight;System.out.println("photoW=" + photoW + "----------" + "photoH="+ photoH);options.inJustDecodeBounds = false;// 计算缩放比例int be = (int) (options.outHeight / 200.0);if (be <= 0)be = 1;options.inSampleSize = 4;// 图片长宽各缩小至四分之一// 重新读入图片,注意这次要把options.inJustDecodeBounds设为falsebitmap = BitmapFactory.decodeFile(path, options);return bitmap;}/*** 释放Bitmap对象 防止内存溢出*/private void destoryBitmap() {if (photo != null && !photo.isRecycled()) {// photo.recycle();photo = null;}}}

PS:手机拍照需要添加权限:<uses-permission android:name="android.permission.CAMERA" />

向SD卡写东西需要权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

运行项目即可成功!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。