项目中使用的---使用系统照相机拍照预览上传


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:gravity="center_horizontal">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用系统照相机拍照" android:onClick="click"/>


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


代码:

package uk.ac.essex.camerademo1;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;

public class Camerademo1Activity extends Activity {
	private static final int CAPTURE_PIC = 0;

	private ImageView imageView;

	private int width;
	private int height;
	private String imageFilePath;
	private Uri imageFileUri;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imageView = (ImageView) findViewById(R.id.imageView);
		init();
	}

	private void init() {
		Display display = getWindowManager().getDefaultDisplay();
		width = display.getWidth();
		height = display.getHeight();

		imageFilePath = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory() + "/1.jpg" : null;
		imageFileUri = Uri.fromFile(new File(imageFilePath));
	}

	public void click(View view) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//相机捕捉图片的意图
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);//指定系统相机拍照保存在imageFileUri所指的位置
		startActivityForResult(intent, CAPTURE_PIC);//启动系统相机,等待返回
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == CAPTURE_PIC) {
			Options options = new Options();
			options.inJustDecodeBounds = true;//设置解码只是为了获取图片的width和height值,而不是真正获取图片
			Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);//解码后可以options.outWidth和options.outHeight来获取图片的尺寸

			int widthRatio = (int) Math.ceil(options.outWidth / width);//获取宽度的压缩比率
			int heightRatio = (int) Math.ceil(options.outHeight / height);//获取高度的压缩比率

			if (widthRatio > 1 || heightRatio > 1) {//只要其中一个的比率大于1,说明需要压缩
				if(widthRatio>=heightRatio){//取options.inSampleSize为宽高比率中的最大值
					options.inSampleSize = widthRatio;
				}else{
					options.inSampleSize = heightRatio;
				}
			}
			
			options.inJustDecodeBounds = false;//设置为真正的解码图片
			bitmap = BitmapFactory.decodeFile(imageFilePath, options);//解码图片

			imageView.setImageBitmap(bitmap);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}


你可能感兴趣的:(android,layout,import,button,encoding,output)