android 相机,图片处理

来源:互联网 发布:js关闭手机输入法 编辑:程序博客网 时间:2024/04/29 23:33

调用系统相机:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);File dir = new File(dirPath);image_name = System.currentTimeMillis() + ".png";if (!dir.exists()) {boolean iscreat = dir.mkdirs();// 创建照片的存储目录MyLog.e(TAG, "" + iscreat);}intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(dir, image_name)));startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);


拍照返回:


@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);MyLog.v(TAG, "resultCode=" + resultCode);if (resultCode == Activity.RESULT_OK) {if (data != null) {MyLog.v(TAG, "item_id:" + data.getIntExtra("item_id", -1));}if (requestCode == REQUEST_CODE_TAKE_PICTURE) {// 拍照返回}}}


图片处理:拍照后的图片有的手机是选择90度的而且图片比较大容易造成内存溢出

public static Bitmap getSmallImage(String pathName, Context context) {MyLog.v(TAG, "getImage");BitmapFactory.Options options = new BitmapFactory.Options();options.inTempStorage = new byte[1024 * 16];//设置图片大小16Koptions.inJustDecodeBounds = true;// 获取这个图片的宽和高Bitmap bitmap = BitmapFactory.decodeFile(pathName, options); // 此时返回bm为空int maxH = SharePreferenceMng.getInstance(context).getScreenWidth() / 4;// 计算缩放比int be = (int) (options.outHeight / (float) maxH);int ys = options.outHeight % maxH;// 求余数float fe = ys / (float) maxH;if (fe >= 0.5)be = be + 1;if (be <= 0)be = 1;options.inSampleSize = be;// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 falseoptions.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeFile(pathName, options);ExifInterface exifInterface;try {exifInterface = new ExifInterface(pathName);int tag = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);if (tag == ExifInterface.ORIENTATION_ROTATE_90) {// 如果是旋转地图片则先旋转,有的相机默认旋转90度Matrix matrix = new Matrix();matrix.reset();matrix.setRotate(90);bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return bitmap;}



0 0
原创粉丝点击