调用照相机

来源:互联网 发布:克珞世网络是真是假 编辑:程序博客网 时间:2024/04/27 17:48
package com.example.camera_d;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Files.FileColumns;
import android.provider.MediaStore.Images.Media;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.camera_d.R.layout;

public class MainActivity extends Activity  {

    private ImageView iv_img;
    private PopupWindow pop;
    private Button btn_opencamera;
    private Button btn_opencmputer;
    private Button btn_over;
    private String path;
    private String path1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        //设置图片保存路径及名字
        path=Environment.getExternalStorageDirectory()+File.separator+"from"+System.currentTimeMillis()+".png";
        path1=Environment.getExternalStorageDirectory()+File.separator+"to"+System.currentTimeMillis()+".png";
        iv_img=(ImageView)findViewById(R.id.iv_img);
        iv_img.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                
                showwindow();
                
            }
        });
        
    }
    
    private void init() {
        GradientDrawable gd = new GradientDrawable(Orientation.LEFT_RIGHT,
                new int[] { Color.GREEN, Color.YELLOW });
        getWindow().setBackgroundDrawable(gd);

        
                    
                    
                    /*//创建popupwindw
                    View view = LayoutInflater.from(this).inflate(layout.popup, null);
                    btn_opencamera=(Button)view.findViewById(R.id.btn_opencamera);
                    btn_opencmputer=(Button)view.findViewById(R.id.btn_opencmputer);
                    btn_over=(Button)view.findViewById(R.id.btn_over);
                    pop=new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    pop.setFocusable(true);
                    pop.setOutsideTouchable(true);
                    pop.setBackgroundDrawable(new BitmapDrawable());
                    btn_opencamera.setOnClickListener(this);
                    btn_opencmputer.setOnClickListener(this);
                    btn_over.setOnClickListener(this);*/
                    
        
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==RESULT_OK){
               switch(requestCode){
            case 1:    
            phonozoom(Uri.fromFile(new File(path)));    
            case 2:
                if(data!=null){phonozoom(data.getData());}
            case 3:
                if(data!=null){setphoto(data);}}}}
    private void showwindow(){    
    
        new AlertDialog.Builder(MainActivity.this).setTitle("设置头像")
        .setNegativeButton("拍照", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                
                Intent it=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                it.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path)));
                startActivityForResult(it, 1);
            }})
        .setPositiveButton("相册", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent it=new Intent(Intent.ACTION_PICK);
                it.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
                startActivityForResult(it, 2);
            }}).show();    }
    
    /**
     * 裁剪图片的方法
     */
    private void phonozoom(Uri uri){
        Intent it=new Intent("com.android.camera.action.CROP");
        it.setDataAndType(uri, "image/*"); // mUri是已经选择的图片Uri  
         it.putExtra("crop", "true");  
         it.putExtra("aspectX", 1);// 裁剪框比例  
         it.putExtra("aspectY", 1);  
         it.putExtra("outputX", 300);// 输出图片大小  
         it.putExtra("outputY", 300);  
         it.putExtra("return-data", true);
         startActivityForResult(it, 3);
    }
    /**
     * 保存裁剪后的图片
     *
     */
    private void setphoto(Intent datat){
        Bundle extras = datat.getExtras();
        if(extras!=null){
            Bitmap bitmap= (Bitmap) extras.getParcelable("data");
            iv_img.setImageBitmap(bitmap);
            try {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(path1));
            } catch (FileNotFoundException e) {e.printStackTrace();}}}
 
}

0 0
原创粉丝点击