在Android的图片库缩放选定的图像 [英] Scale selected image in Android image gallery

查看:129
本文介绍了在Android的图片库缩放选定的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于按比例缩小的图片库选择的图像,这样我就可以通过PHP把它上传到服务器的问题。

I have a question regarding scaling down the selected image in image gallery so i can upload it to a server via php.

所以动作的顺序是:

1)获取选定的图像从画廊(DONE)

1)Get selected image from gallery (DONE)

2)规模下来,COM preSS它JPG(GOT stucked这里)

2)Scale it down and compress it to jpg (Got stucked here)

3)上传到服务器异步(DONE)

3)Upload to server async(DONE)

我需要连接1和第3 step.So到目前为止,我已经做到了这一点:

I need to connect 1st and 3rd step.So far i have done this:

获取所选图像:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            Bitmap yourSelectedImage = BitmapFactory.decodeFile(picturePath); 

            new async_upload().execute(picturePath.toString());
        }

    }

异步上传:

public class async_upload extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... arg0) {
            uploadFile(arg0[0]);
            return "asd";
        }

        @Override       
        protected void onPostExecute(String result) {

        }
    } 

     public int uploadFile(String sourceFileUri) {
..............Code for uploading works.......
}

和我的功能,缩小图像。

And my function for scaling down the image.

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 640;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
               || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

    }

正如你可以看到我发送所选图像的URI来 uploadFile 来上传异步。 我的问题是我怎么能做副本选择的图像,COM preSS /规模和发送的原始这一形象来代替。当然,我不希望改变用户的形象......但送缩放版本。

As you can see i am sending the URI of the selected image to uploadFile to be uploaded async. My problem is how can i "make a copy" of the selected image,compress/scale and send this image instead of the original one. Of course i dont want to alter the user's images...but send a scaled version.

任何想法? 谢谢!

推荐答案

您可以做这些方针的东西

you can do something along these lines

File pngDir = new   File(Environment.getExternalStorageDirectory(),"PicUploadTemp");
               if (!pngDir.exists()) {
            pngDir.mkdirs();
            }
             pngfile = new File(pngDir,"texture1.jpg");

             FileOutputStream fOut;
            try {
                    fOut = new FileOutputStream(pngfile);
                     finalbitmap.compress(Bitmap.CompressFormat.JPG, 100,fOut);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            pngUri = Uri.fromFile(pngfile);

为code你把第一个这种方法添加到您的类

for the code you put up first add this method to your class

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
    }

那么,你只要把你的code,像这样

then in your code that you just put up, do it like this

BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(picturePath, options);
                int imageHeight = options.outHeight;
                int imageWidth = options.outWidth;
                String imageType = options.outMimeType;
                    options.inSampleSize = calculateInSampleSize(options,512,256);//512 and 256 whatever you want as scale
                    options.inJustDecodeBounds = false;
                Bitmap yourSelectedImage = BitmapFactory.decodeFile(selectedImagePath,options);
//Bitmap yourSelectedImage = BitmapFactory.decodeFile(picturePath);

            File pngDir = new   File(Environment.getExternalStorageDirectory(),"PicUploadTemp");
            if (!pngDir.exists()) {
            pngDir.mkdirs();
         }

          File pngfile = new File(pngDir,"texture1.jpg");
            FileOutputStream fOut;

            try {
                    fOut = new FileOutputStream(pngfile);

                     yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 50,fOut);


            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            new async_upload().execute(pngfile.getPath().toString());
            yourSelectedImage.recycle();
            resizedBitmap.recycle();
            Log.d("INTERNET", yourSelectedImage.toString());

这篇关于在Android的图片库缩放选定的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆