Android_相機(jī)、相冊(cè)

相冊(cè)

調(diào)起系統(tǒng)相機(jī)

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

ACTION_PICK, ACTION_GET_CONTENT,后者主要是根據(jù) type 來(lái)調(diào)用相應(yīng)的程序
在onActivityResult中響應(yīng)回調(diào)

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;
}

相機(jī)

調(diào)起系統(tǒng)相機(jī)

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);  // 設(shè)置了此值會(huì)在臨時(shí)uri中存下原圖,intent是空值
startActivityForResult(intent, REQUEST_CODE_CAMRE); 

回調(diào)中

// 獲取的是縮略圖
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();
}

如偏角度問(wèn)題、旋轉(zhuǎn)

/**
 * 獲取圖片的存儲(chǔ)角度
 * @param path
 * @return
 */
private int getBitmapDegree(String path) {
    int degree = 0;
    try {
        // 從指定路徑下讀取圖片,并獲取其EXIF信息
        ExifInterface exifInterface = new ExifInterface(path);
        // 獲取圖片的旋轉(zhuǎn)信息
        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;
}

/**
 * 旋轉(zhuǎn)bitmap
 * @param bm
 * @param degree
 * @return
 */
public Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
    Bitmap returnBm = null;
    // 根據(jù)旋轉(zhuǎn)角度,生成旋轉(zhuǎn)矩陣
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    try {
        // 將原始圖片按照旋轉(zhuǎn)矩陣進(jìn)行旋轉(zhuǎn),并得到新的圖片
        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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容