import android.app.Activity; import android.app.AlertDialog; import android.content.ActivityNotFoundException; 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.text.format.DateFormat; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; import com.example.administrator.myapplication.image.PorterDuffViewImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import java.util.Locale; public class MainActivity extends Activity implements View.OnClickListener{ private static final int CAMERA_WITH_DATA = 3023; private static final int UPLOAD_IMAGE = 1 ; String path; private String picPath; /* 用来标识请求gallery的activity */ /* 拍照的照片存储位置 */ public static final File PHOTO_DIR = new File( Environment.getExternalStorageDirectory() + "/DCIM/Camera"); private PorterDuffViewImage porter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_activity_main); porter = (PorterDuffViewImage) findViewById(R.id.porter); porter.setImageResource(R.mipmap.h11); findViewById(R.id.btn_id).setOnClickListener(this); } private void initDate(){ } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_id: showCustomAlertDialog(); } } private void showCustomAlertDialog(){ final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.show(); Window win = alertDialog.getWindow(); WindowManager.LayoutParams lp = win.getAttributes(); win.setGravity(Gravity.LEFT | Gravity.BOTTOM); lp.alpha = 0.7f; win.setAttributes(lp); win.setContentView(R.layout.dialog); Button camera_camera = (Button) win.findViewById(R.id.camera_camera); Button camera_phone = (Button) win.findViewById(R.id.camera_phone); Button camera_cancel = (Button) win.findViewById(R.id.camera_cancel); camera_camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { alertDialog.dismiss(); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/my_photo.jpg"))); startActivityForResult(intent, 1); } }); camera_phone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { alertDialog.dismiss(); Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, 2); } }); camera_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { alertDialog.dismiss(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode !=RESULT_OK) return; switch (requestCode){ case 1: File temp = new File(Environment.getExternalStorageDirectory() + "/my_photo.jpg"); startPhotoZoom(Uri.fromFile(temp)); break; case 2: startPhotoZoom(data.getData()); break; case 3: Bitmap bitm = getBitmap(data); porter.setImageBitmap(bitm); } super.onActivityResult(requestCode, resultCode, data); } public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("return-data", true); startActivityForResult(intent, 3); } private void doTakePhoto(){ try{ path = Environment.getExternalStorageDirectory() + "/+" + 123 + ".jpg"; final Intent intent = getTakePickIntent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path))); startActivityForResult(intent, CAMERA_WITH_DATA); }catch (ActivityNotFoundException e) { Toast.makeText(this, "获取照片错误", Toast.LENGTH_LONG).show(); } } private void doCropPhoto(Intent data){ Uri currImageURI = null; if (data != null) { if (data.getExtras() != null) { File file = getFile(getBitmap(data)); // File mCurrentPhotoFile = new File(PHOTO_DIR, // getPhotoFileName());// // 给新照的照片文件命名 currImageURI = Uri.fromFile(file); } else { currImageURI = data.getData(); } } else { currImageURI = Uri.fromFile(new File(path)); } try { // 启动gallery去剪辑这个照片 final Intent intent = getCropImageIntent(currImageURI); startActivityForResult(intent, UPLOAD_IMAGE); } catch (Exception e) { Toast.makeText(this, "获取照片错误", Toast.LENGTH_LONG).show(); } } private static Intent getCropImageIntent(Uri photoUri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(photoUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("return-data", true); return intent; } private Bitmap getBitmap(Intent data) { Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式 return bitmap; } private File getFile(Bitmap bitmap) { String sdStatus = Environment.getExternalStorageState(); if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用 return null; } String name = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg"; FileOutputStream b = null; if (!PHOTO_DIR.isDirectory()) { PHOTO_DIR.mkdirs();// 创建文件夹 } File fileName = new File(PHOTO_DIR, name); try { b = new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.PNG, 100, b);// 把数据写入文件 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { b.flush(); b.close(); } catch (IOException e) { e.printStackTrace(); } } return fileName; } private static Intent getTakePickIntent(String action) { Intent intent = new Intent(); intent.putExtra("return-data", true); intent.setAction(action); // intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); return intent; } // 请求Gallery程序 private void doPickPhotoFromGallery() { try { // Launch picker to choose photo for selected contact final Intent intent = getPhotoPickIntent(); startActivityForResult(intent, UPLOAD_IMAGE); } catch (ActivityNotFoundException e) { Toast.makeText(this, "获取照片错误", Toast.LENGTH_LONG).show(); } } // 封装请求Gallery的intent private static Intent getPhotoPickIntent() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 10); intent.putExtra("aspectY", 10); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("return-data", true); return intent; } }
调用相机和手机拍照
最新推荐文章于 2019-06-15 08:06:46 发布