android图片处理 ---获取图片

intent详解(一)intent详解(二)

1、获取图片

1、相册
以隐氏intent的方式打开系统默认的图库,需要传入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,打开系统的相机

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应用程序,不会将全尺寸的图片传递给主调程序:系统为了防止应用内存占用过大,对于在应用内通过相机拍摄的图片最终返回来的结果进行了压缩,压缩后的图片变得很小,如下:

默认的,不传递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);
  • 创建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中调用裁剪图片的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中的回调

不管是拍照还是通过相册,总有办法得到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:

    /**
     * 根据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,网络传输时是stream,内存中是stream或bitmap

质量压缩,它其实只能实现对file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width * height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小了;

尺寸压缩,由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;

  • 质量压缩
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方法获取到照片数据后,读取exif信息,将照片旋转到正确的方向(Matrix)。
什么是Exif?
Exif是一种图像文件格式,是在JPEG格式头插入了照片的信息。通过ExifInterface类可以操作图片的Exif信息,其中定义了一些字符串的静态常量:
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信息后,调用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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容