|
| 1 | +package com.clock.study.helper; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Intent; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.content.pm.ResolveInfo; |
| 7 | +import android.net.Uri; |
| 8 | +import android.provider.MediaStore; |
| 9 | +import android.widget.Toast; |
| 10 | + |
| 11 | +import com.clock.study.R; |
| 12 | +import com.clock.utils.bitmap.BitmapUtils; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.text.SimpleDateFormat; |
| 17 | +import java.util.Date; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +/** |
| 21 | + * 拍照辅助类 |
| 22 | + * <p/> |
| 23 | + * Created by Clock on 2016/5/21. |
| 24 | + */ |
| 25 | +public class CapturePhotoHelper { |
| 26 | + |
| 27 | + private final static String TIMESTAMP_FORMAT = "yyyy_MM_dd_HH_mm_ss"; |
| 28 | + |
| 29 | + public final static int CAPTURE_PHOTO_REQUEST_CODE = 0x1111; |
| 30 | + |
| 31 | + private Activity mActivity; |
| 32 | + /** |
| 33 | + * 存放图片的目录 |
| 34 | + */ |
| 35 | + private File mPhotoFolder; |
| 36 | + /** |
| 37 | + * 拍照生成的图片文件 |
| 38 | + */ |
| 39 | + private File mPhotoFile; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param activity |
| 43 | + * @param photoFolder 存放生成照片的目录,目录不存在时候会自动创建,但不允许为null; |
| 44 | + */ |
| 45 | + public CapturePhotoHelper(Activity activity, File photoFolder) { |
| 46 | + this.mActivity = activity; |
| 47 | + this.mPhotoFolder = photoFolder; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * 拍照 |
| 52 | + */ |
| 53 | + public void capture() { |
| 54 | + if (hasCamera()) { |
| 55 | + createPhotoFile(); |
| 56 | + |
| 57 | + if (mPhotoFile == null) { |
| 58 | + Toast.makeText(mActivity, R.string.camera_open_error, Toast.LENGTH_SHORT).show(); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 63 | + Uri fileUri = Uri.fromFile(mPhotoFile); |
| 64 | + captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); |
| 65 | + mActivity.startActivityForResult(captureIntent, CAPTURE_PHOTO_REQUEST_CODE); |
| 66 | + |
| 67 | + } else { |
| 68 | + Toast.makeText(mActivity, R.string.camera_open_error, Toast.LENGTH_SHORT).show(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 创建照片文件 |
| 74 | + */ |
| 75 | + private void createPhotoFile() { |
| 76 | + if (mPhotoFolder != null) { |
| 77 | + if (!mPhotoFolder.exists()) {//检查保存图片的目录存不存在 |
| 78 | + mPhotoFolder.mkdirs(); |
| 79 | + } |
| 80 | + |
| 81 | + String fileName = new SimpleDateFormat(TIMESTAMP_FORMAT).format(new Date()); |
| 82 | + mPhotoFile = new File(mPhotoFolder, fileName + BitmapUtils.JPG_SUFFIX); |
| 83 | + if (mPhotoFile.exists()) { |
| 84 | + mPhotoFile.delete(); |
| 85 | + } |
| 86 | + try { |
| 87 | + mPhotoFile.createNewFile(); |
| 88 | + } catch (IOException e) { |
| 89 | + e.printStackTrace(); |
| 90 | + mPhotoFile = null; |
| 91 | + } |
| 92 | + } else { |
| 93 | + mPhotoFile = null; |
| 94 | + Toast.makeText(mActivity, R.string.not_specify_a_directory, Toast.LENGTH_SHORT).show(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * 判断系统中是否存在可以启动的相机应用 |
| 101 | + * |
| 102 | + * @return 存在返回true,不存在返回false |
| 103 | + */ |
| 104 | + public boolean hasCamera() { |
| 105 | + PackageManager packageManager = mActivity.getPackageManager(); |
| 106 | + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 107 | + List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); |
| 108 | + return list.size() > 0; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * 获取当前拍到的图片文件 |
| 113 | + * |
| 114 | + * @return |
| 115 | + */ |
| 116 | + public File getPhoto() { |
| 117 | + return mPhotoFile; |
| 118 | + } |
| 119 | +} |
0 commit comments