Android_相机、相册

2016-01-14  本文已影响198人  738bc070cd74

相册

调起系统相机

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");// 相片类型
startActivityForResult(intent, REQUEST_CODE_IMAGE);

ACTION_PICK, ACTION_GET_CONTENT,后者主要是根据 type 来调用相应的程序
在onActivityResult中响应回调

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if(resultCode == RESULT_OK) {
        Bitmap bitmap = null;
        if (requestCode == REQUEST_CODE_IMAGE) {
            Uri uri = intent == null ? null : intent.getData();
            if(uri != null) {
                String img_path = getPathFromUri(uri);
                bitmap = getBitmapByImgPath(img_path);
            }
        }
}

private String getPathFromUri(Uri uri) {
    String img_path = "";
    if(uri != null){
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);
        int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualimagecursor.moveToFirst();
        img_path = actualimagecursor.getString(actual_image_column_index);
    }
    return img_path;
}

// 获取bitmap
private Bitmap getBitmapByImgPath(String img_path) {
    Bitmap photo = null;
    if(!TextUtils.isEmpty(img_path)) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(img_path, opt);
        LogUtils.d("outWidth"+opt.outWidth);
        LogUtils.d("outHeight"+opt.outHeight);
        if (opt.outWidth > 1000 || opt.outHeight > 1000) {
            opt.inSampleSize = 5;
        }
        opt.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(img_path, opt);
        int degree = getBitmapDegree(img_path);
        photo = rotateBitmapByDegree(bitmap, degree);
    }
    return photo;
}

相机

调起系统相机

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);  // 设置了此值会在临时uri中存下原图,intent是空值
startActivityForResult(intent, REQUEST_CODE_CAMRE); 

回调中

// 获取的是缩略图
Bundle bundle = intent.getExtras();
if (bundle != null) {
     Bitmap photo = (Bitmap) bundle.get("data");
     Uri bitmapUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photo, null,null));
                
     img_path = getPathFromUri(bitmapUri);
 } 
            

// 获取原图
if(mUri != null) {                        
    this.getContentResolver().notifyChange(mUri, null);
    ContentResolver cr = this.getContentResolver();
try {
    bitmap = Media.getBitmap(cr, mUri);// 获取原图
           // 推荐获取流,然后获取指定大小
           InputStream input = getContentResolver().openInputStream(uri);
                    
} catch (Exception e) {
    e.printStackTrace();
}

如偏角度问题、旋转

/**
 * 获取图片的存储角度
 * @param path
 * @return
 */
private int getBitmapDegree(String path) {
    int degree = 0;
    try {
        // 从指定路径下读取图片,并获取其EXIF信息
        ExifInterface exifInterface = new ExifInterface(path);
        // 获取图片的旋转信息
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}

/**
 * 旋转bitmap
 * @param bm
 * @param degree
 * @return
 */
public Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
    Bitmap returnBm = null;
    // 根据旋转角度,生成旋转矩阵
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    try {
        // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
        returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
    } catch (OutOfMemoryError e) {
    }
    if (returnBm == null) {
        returnBm = bm;
    }
    if (bm != returnBm) {
        bm.recycle();
    }
    return returnBm;
}
上一篇下一篇

猜你喜欢

热点阅读