android圖片處理 ---獲取圖片

intent詳解(一)intent詳解(二)

1、獲取圖片

1、相冊
以隱氏intent的方式打開系統(tǒng)默認的圖庫,需要傳入mimeType

com.cooliris.media.Gallery

代碼如下:

//打開圖片
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
//Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, REQUEST_GALLERY);

1、ACTION_PICK、ACTION_GET_CONTENT可以完成相似的功能
2、mimeType:該activity可以處理的文件類型,形式:[type]/[subtype]。 Android MimeType的用途以及所有類型

在onActivityResult中,可以通過以下方法得到Uri:

Uri uri = data.getData();

2、相機
同樣使用隱式的intent,打開系統(tǒng)的相機

com.android.camera.Camera

1、使用默認的返回路徑
代碼如下:

Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,REQUEST_CAMERA);
The Camera activity which can preview and take pictures.

所以,在onActivityResult中,可以通過以下方法得到圖片:

Bundle bundle=data.getExtras();
Bitmap bitmap= (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);

但是,camera應用程序,不會將全尺寸的圖片傳遞給主調(diào)程序:系統(tǒng)為了防止應用內(nèi)存占用過大,對于在應用內(nèi)通過相機拍攝的圖片最終返回來的結果進行了壓縮,壓縮后的圖片變得很小,如下:

默認的,不傳遞EXTRA_OUTPUT,返回的圖片被壓縮

2、傳遞給camera應用程序,一個路徑
為了得到期望的圖片,可以為camera應用程序,傳遞一個附加值,這個附加值的名稱在MediaStore中指定:EXTRA_OUTPUT,以URI的形式指示捕獲的圖像放置的位置(imgUri)

Camera的onCreate方法中得到mSaveUri,指示捕獲的圖片的路徑
//傳遞給camera應用程序,一個附加值
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intent,REQUEST_CAMERA);
  • 創(chuàng)建Uri
  • 1、使用ContentResolver,因為是添加圖片,用insert
    Uri imgUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
  • 2、 android文件存儲路徑
    String imgPath= Environment.getExternalStorageDirectory().getAbsolutePath()+"/_"+System.currentTimeMillis();
    Uri uri=Uri.parse(imgPath);

注意:權限,android.permission.WRITE_EXTERNAL_STORAGE

Camera返回圖片,mSaveUri是路徑

2、裁剪

可以在onActivityResult中調(diào)用裁剪圖片的intent,如下:

Intent cropIntent=new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(srcUri,"image/*");
cropIntent.putExtra("scale", true);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
startActivityForResult(cropIntent,IMG_CROP);
QQ圖片20160804160705.png

3、onActivityResult中的回調(diào)

不管是拍照還是通過相冊,總有辦法得到Uri,通過這個Uri就可以取得bitmap或者imgPath

取得bitmap:

Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri), null, null);

或者

Bitmap bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),imgUri);

或者

Bitmap bitmap= BitmapFactory.decodeFile(imgPath, null);

取得imgPath:

    /**
     * 根據(jù)Uri轉變成真實路徑
     */
    public String getRealFilePath(Uri uri) {
        String scheme=uri.getScheme();
        if(scheme==null || scheme.equals(ContentResolver.SCHEME_FILE)){
            String p= uri.getPath();
            return p;
        }
        Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
        if (cursor == null) {
            return null;
        }
        if (cursor.moveToFirst()) {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            cursor.close();
        }
        return null;
    }

之后,對得到的bitmap進行壓縮:

4、壓縮

圖片有三種存在形式:硬盤上時是file,網(wǎng)絡傳輸時是stream,內(nèi)存中是stream或bitmap

質(zhì)量壓縮,它其實只能實現(xiàn)對file的影響,你可以把一個file轉成bitmap再轉成file,或者直接將一個bitmap轉成file時,這個最終的file是被壓縮過的,但是中間的bitmap并沒有被壓縮(或者說幾乎沒有被壓縮,我不確定),因為bigmap在內(nèi)存中的大小是按像素計算的,也就是width * height,對于質(zhì)量壓縮,并不會改變圖片的像素,所以就算質(zhì)量被壓縮了,但是bitmap在內(nèi)存的占有率還是沒變小,但你做成file時,它確實變小了;

尺寸壓縮,由于是減小了圖片的像素,所以它直接對bitmap產(chǎn)生了影響,當然最終的file也是相對的變小了;

  • 質(zhì)量壓縮
public String qualityCompressImg(Bitmap bitmap, String outPath, int maxSize) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    float option = 100f;
    bitmap.compress(Bitmap.CompressFormat.JPEG, (int) option, byteArrayOutputStream);
    while (byteArrayOutputStream.toByteArray().length / 1024 > maxSize) {
        byteArrayOutputStream.reset();
        option *= 0.9f;
        if(option<1){
            break;
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, (int) option, byteArrayOutputStream);
    }
    try {
        FileOutputStream outputStream = new FileOutputStream(outPath);
        outputStream.write(byteArrayOutputStream.toByteArray());
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
   return outPath;
}
  • 尺寸壓縮
public Bitmap ratioBitmap(String path, int viewWidth, int viewHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    options.inJustDecodeBounds = true;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    BitmapFactory.decodeFile(path, options);
    int h = options.outHeight;
    int w = options.outWidth;
    int inSampleSize = (int) Math.max(h * 1.0 / viewWidth, w * 1.0 / viewHeight);
    if (inSampleSize <= 0) {
        inSampleSize = 1;
    }
    options.inJustDecodeBounds = false;
    options.inSampleSize = inSampleSize;
    return BitmapFactory.decodeFile(path, options);
}

5、操作Exif信息

為什么在有些手機上拍攝的照片看起來方向不對?
用相機拍攝出來的照片含有EXIF信息,ExifInterface.TAG_ORIENTATION指的就是EXIF中的orientation信息。如果我們忽略orientation信息,直接去獲取圖片的bitmap,得到的結果會旋轉(90°、180°、270°)。所以在onActivityResult方法獲取到照片數(shù)據(jù)后,讀取exif信息,將照片旋轉到正確的方向(Matrix)。
什么是Exif?
Exif是一種圖像文件格式,是在JPEG格式頭插入了照片的信息。通過ExifInterface類可以操作圖片的Exif信息,其中定義了一些字符串的靜態(tài)常量:
TAG_APERTURE:光圈值。
TAG_DATETIME:拍攝時間,取決于設備設置的時間。
TAG_EXPOSURE_TIME:曝光時間。
TAG_FLASH:閃光燈。
TAG_FOCAL_LENGTH:焦距。
TAG_IMAGE_LENGTH:圖片高度。
TAG_IMAGE_WIDTH:圖片寬度。
TAG_ISO:ISO。
TAG_MAKE:設備品牌。
TAG_MODEL:設備型號,整形表示,在ExifInterface中有常量對應表示。
TAG_ORIENTATION:旋轉角度,整形表示,在ExifInterface中有常量對應表示。

使用setAttribute()設置Exif信息,將不會寫入到目標圖片中,只有在改變Exif信息后,調(diào)用saveAttribute()才可以把新的Exif寫入到目標圖片中。

代碼如下:
讀取exif信息:

ExifInterface oldExif=new ExifInterface(pathImage);
Class<ExifInterface> exifInterfaceClass=ExifInterface.class;
Field[] fields=exifInterfaceClass.getFields();
for(int i=0;i<fields.length;i++){
    String fieldName = fields[i].getName();
    if (!TextUtils.isEmpty(fieldName) && fieldName.startsWith("TAG")) {
        String fieldValue = fields[i].get(exifInterfaceClass).toString();
        String attribute = oldExif.getAttribute(fieldValue);
        L.e("exif",fieldName+"-"+fieldValue+"-"+attribute);
    }

}

旋轉圖片:

    public String rotateImg(String imgPath, int maxSize) {
        Bitmap bitmap=BitmapFactory.decodeFile(imgPath, null);
        int rotate=0;
        try {
            ExifInterface old = new ExifInterface(imgPath);
            int orientation=old.getAttributeInt(ExifInterface.TAG_ORIENTATION,0);
            if(orientation==ExifInterface.ORIENTATION_ROTATE_90){
                rotate=90;
            }else if(orientation==ExifInterface.ORIENTATION_ROTATE_270){
                rotate=270;
            }else if(orientation==ExifInterface.ORIENTATION_ROTATE_180){
                rotate=180;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Matrix matrix=new Matrix();
        matrix.setRotate(rotate, bitmap.getWidth()/2,bitmap.getHeight()/2);
        Bitmap tempBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);

        if(tempBitmap!=bitmap){
            bitmap.recycle();
        }

        return qualityCompressImg(tempBitmap, imgPath, maxSize);
    }

如果rotate=0,recycle(),會有問題?
因為 tempBitmap和bitmap可能會是同一個

Bitmap.createBitmap,源bitmap和目標bitmap可能是同一個

http://www.cnblogs.com/plokmju/p/android_exif.html
http://blog.csdn.net/berber78/article/details/39778181
http://blog.csdn.net/u012816041/article/details/50602246

6、七牛 上傳圖片

new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url=new URL("http://101.201.211.229/zbhq/Home/BabyShow/upToken");
                    HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                    connection.connect();
                    if(connection.getResponseCode()==200){
                        InputStream inputStream=connection.getInputStream();
                        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
                        byte[] buff=new byte[1024];
                        int len=0;
                        while ((len=inputStream.read(buff))!=-1){
                            outputStream.write(buff,0,len);
                        }
                        String jsonString=outputStream.toString();
                        outputStream.close();
                        inputStream.close();
                        JSONObject jsonObject=new JSONObject(jsonString);
                        final String code=jsonObject.getString("code");
                        final String uptoken=jsonObject.getString("uptoken");
                        UploadManager uploadManager=new UploadManager();
                        String key="babyShow/" +System.currentTimeMillis();
                        uploadManager.put(uploadPath, key, uptoken, new UpCompletionHandler() {
                            @Override
                            public void complete(String key, ResponseInfo info, JSONObject response) {
                                final String path="http://7xrpiy.com1.z0.glb.clouddn.com/"+key;
                                text2.setText(code + ":" + path);
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            URL url1=new URL(path);
                                            HttpURLConnection connection1= (HttpURLConnection) url1.openConnection();
                                            if(connection1.getResponseCode()==200){
                                                InputStream inputStream1=connection1.getInputStream();
                                                final Bitmap bitmap=BitmapFactory.decodeStream(inputStream1);
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        imageView2.setImageBitmap(bitmap);
                                                    }
                                                });
                                                inputStream1.close();
                                            }
                                        } catch (MalformedURLException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                            }
                        }, null);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }).start();
七牛 上傳圖片

代碼:
https://coding.net/u/hongji/p/TakePictureDemo/git

參考:
http://blog.csdn.net/jdsjlzx/article/details/44228935
http://blog.csdn.net/floodingfire/article/details/8144604

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,565評論 6 539
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,115評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 177,577評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,514評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,234評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,621評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,641評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,822評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,380評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,128評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,319評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,879評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,548評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,970評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,229評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,048評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,285評論 2 376

推薦閱讀更多精彩內(nèi)容