首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android拍照与相片

2012-07-03 
Android照相与相片public class ShootMainActivity extends Activity implements OnClickListener {// 用

Android照相与相片

public class ShootMainActivity extends Activity implements OnClickListener {// 用来标识请求照相功能的activityprivate static final int CAMERA_WITH_DATA = 3020;// 用来标识请求gallery的activityprivate static final int PHOTO_PICKED_WITH_DATA = 3021;private File mCurrentPhotoFile;// 照相机拍照得到的图片private int DECIDE;private Button mPhotoButton; // 拍照private Button mAlbumButton; // 相册private ImageView mImage;private Uri mImageUri;private Uri uri;private byte[] mBuffer; // 保存拍照的字节信息private boolean hasPic = false;private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);initUI();}private void initUI() {mPhotoButton = (Button) this.findViewById(R.id.photobutton);mPhotoButton.setOnClickListener(this);mAlbumButton = (Button) this.findViewById(R.id.albumbutton);mAlbumButton.setOnClickListener(this);mImage = (ImageView) this.findViewById(R.id.image);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.photobutton:doTakePhoto();break;case R.id.albumbutton:doPickPhotoFromGallery();break;}}// 拍照的照片存储位置public String getPhotoDir() {return Environment.getExternalStorageDirectory() + "/DCIM/Camera/1.jpg";}// 拍照获取图片protected void doTakePhoto() {PHOTO_DIR.mkdirs();mCurrentPhotoFile = new File(getPhotoDir());// 给新照的照片文件命名final Intent mIntent = getPhotoIntent(mCurrentPhotoFile);startActivityForResult(mIntent, CAMERA_WITH_DATA);}// 请求Gallery程序protected void doPickPhotoFromGallery() {final Intent mIntent = getPhotoAlbumIntent();startActivityForResult(mIntent, PHOTO_PICKED_WITH_DATA);}// 封装请求拍照的intentpublic static Intent getPhotoIntent(File f) {// Intent intent = new Intent("android.media.action.IMAGE_CAPTURE",// null);Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));Bundle bundle = new Bundle();bundle.putString("cascde", "19921231010");intent.putExtras(bundle);return intent;}// 封装请求Gallery的intentpublic static Intent getPhotoAlbumIntent() {Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType("image/*");// intent.putExtra("crop", "true");// intent.putExtra("aspectX", 1);// intent.putExtra("aspectY", 1);// intent.putExtra("outputX", 80);// intent.putExtra("outputY", 80);intent.putExtra("return-data", true);return intent;}protected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case CAMERA_WITH_DATA:imageByteData(data);break;case PHOTO_PICKED_WITH_DATA:imageUri(data);break;}imageData();}private void imageData() {if (!hasPic)return;else {switch (DECIDE) {case CAMERA_WITH_DATA:Bitmap mbBitmap = getUri2Bitmap(uri);// mImage.setImageURI(uri);mImage.setImageBitmap(mbBitmap);break;case PHOTO_PICKED_WITH_DATA:mImage.setImageURI(mImageUri);break;}}}// 将uri转成bitmapprivate Bitmap getUri2Bitmap(Uri uriTo) {ContentResolver resolver = getContentResolver();try {// 将图片内容解析成字节数组mBuffer = readStream(resolver.openInputStream(uriTo));} catch (Exception e) {e.printStackTrace();}// 将字节数组转换为ImageView可调用的Bitmap对象Bitmap mBitmap = getPicFromBytes(mBuffer, null);return mBitmap;}// 将字节数组转换为ImageView可调用的Bitmap对象public static Bitmap getPicFromBytes(byte[] bytes,BitmapFactory.Options opts) {if (bytes != null)if (opts != null)return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);elsereturn BitmapFactory.decodeByteArray(bytes, 0, bytes.length);return null;}// 将图片内容解析成字节数组public static byte[] readStream(InputStream inStream) throws Exception {byte[] buffer = new byte[1024];int len = -1;ByteArrayOutputStream outStream = new ByteArrayOutputStream();while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}byte[] data = outStream.toByteArray();outStream.close();inStream.close();return data;}private void imageUri(Intent data) {mImageUri = data.getData();this.hasPic = true;this.DECIDE = PHOTO_PICKED_WITH_DATA;}public void imageByteData(Intent data) {if (mCurrentPhotoFile == null)mCurrentPhotoFile = new File(getPhotoDir());uri = Uri.fromFile(mCurrentPhotoFile);this.hasPic = true;this.DECIDE = CAMERA_WITH_DATA;}}

?

热点排行
Bad Request.