android - 如何正确压缩此位图?

标签 android bitmap

我想从相机/画廊获取图像,将其压缩并发送到 Firebase。

所以我做了这个(selectImage 是从相机/图库加载的函数):

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

    if(resultCode == RESULT_OK) {
        if(requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) { //dalla fotocamera
            if(data != null) { //caso galleria
                mMediaUri = data.getData();
            }

            CompressBitmap task = new CompressBitmap(this);
            String newPath=null;
            try {
                newPath = task.execute(String.valueOf(mMediaUri)).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

            Picasso.with(this).load(newPath).into(addPhoto, new Callback() {

                public void onSuccess() {
                    // stopProgressDialog(circleDialog);
                    showErrorMessage(RegisterActivity.this,getString(R.string.okAddPhoto));
                }

                @Override
                public void onError() {
                    showErrorMessage(RegisterActivity.this,getString(R.string.errAddPhoto));
                }
            });
        }
    } else {

    }
}

private void selectImage() {
    final CharSequence[] items = {getString(R.string.camera), getString(R.string.gallery), getString(R.string.indietro)};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.addPhoto);
    builder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals(getString(R.string.camera))) {
                mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

                Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                startActivityForResult(photoIntent, REQUEST_TAKE_PHOTO);
                dialog.dismiss();
            } else if (items[item].equals(getString(R.string.gallery))) {
                Intent pickPhoto = new Intent(Intent.ACTION_GET_CONTENT);
                pickPhoto.setType("image/*");
                startActivityForResult(pickPhoto,REQUEST_PICK_PHOTO);
                dialog.dismiss();
            } else if (items[item].equals(getString(R.string.indietro))) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

private Uri getOutputMediaFileUri(int mediaTypeImage) {
    //check for external storage
    if(isExternalStorageAvaiable()) {
        File mediaStorageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

       String fileName = "";
       String fileType = "";
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());

       fileName = "IMG_"+timeStamp;
       fileType = ".jpg";

       File mediaFile;
       try {
           mediaFile = File.createTempFile(fileName,fileType,mediaStorageDir);
           Log.i("st","File: "+Uri.fromFile(mediaFile));
       } catch (IOException e) {
           e.printStackTrace();
           Log.i("St","Error creating file: " + mediaStorageDir.getAbsolutePath() +fileName +fileType);
           return null;
       }
       return Uri.fromFile(mediaFile);
   }
   //something went wrong
   return null;
}

private boolean isExternalStorageAvaiable() {
    String state = Environment.getExternalStorageState();

    if(Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    } else {
        return false;
    }
}

我只有在加载大图像时才会遇到问题。所以我认为我应该在将位图图像加载到 ImageView 之前压缩它。所以我找到了这段代码并使用了它:

private class CompressBitmap extends AsyncTask<String, Void, String> {

    private Context context;
    private static final float maxHeight = 1280.0f;
    private static final float maxWidth = 1280.0f;


    public CompressBitmap(Context context) {
        this.context=context;
    }

    protected String doInBackground(String... strings) {
        if(strings.length == 0 || strings[0] == null) {
            return null;
        } return compressImage(strings[0]);
    }

    public String compressImage(String imagePath) {
        Bitmap scaledBitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;
        float imgRatio = (float) actualWidth / (float) actualHeight;
        float maxRatio = maxWidth / maxHeight;

        Log.d("actual height, width:", String.valueOf(actualHeight) + "," + String.valueOf(actualWidth));
        Log.d("imgRatio,maxRatio", String.valueOf(imgRatio)+","+String.valueOf(maxRatio));

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;
            }
        }

        options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];

        try {
            bmp = BitmapFactory.decodeFile(imagePath, options);
            Log.d("bmp",String.valueOf(bmp));
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }

        try {
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
            Log.d("scaledBitmap",String.valueOf(scaledBitmap));
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }

        float ratioX = actualWidth / (float) options.outWidth;
        float ratioY = actualHeight / (float) options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;
        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

        if (bmp != null) {
            bmp.recycle();
        }

        ExifInterface exif;

        try {
            exif = new ExifInterface(imagePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.postRotate(90);
            } else if (orientation == 3) {
                matrix.postRotate(180);
            } else if (orientation == 8) {
                matrix.postRotate(270);
            }
            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileOutputStream out = null;
        String filepath = imagePath;//getFilename();
        try {
            //new File(imageFilePath).delete();
            out = new FileOutputStream(filepath);

            //write the compressed bitmap at the destination specified by filename.
            scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return filepath;
    }

    public String getFilename() {
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                + "/ImageCompApp/Images");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
        String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
        return uriString;
    }
}

public static String getFilename() {
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/ImageCompApp/Images");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        mediaStorageDir.mkdirs();
    }

    String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
    String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
    return uriString;
}

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

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    final float totalPixels = width * height;
    final float totalReqPixelsCap = reqWidth * reqHeight * 2;

    while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
        inSampleSize++;
    }
    return inSampleSize;
}

但我收到此错误消息:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A6174: open failed: ENOENT (No such file or directory)
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A6174: open failed: ENOENT (No such file or directory)
W/System.err: java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: width and height must be > 0
W/System.err:     at java.util.concurrent.FutureTask.report(FutureTask.java:94)
W/System.err:     at java.util.concurrent.FutureTask.get(FutureTask.java:164)
W/System.err:     at android.os.AsyncTask.get(AsyncTask.java:498)
W/System.err:     at com.sfproduction.happypark.RegisterActivity.onActivityResult(RegisterActivity.java:566)
W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6456)
W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
W/System.err:     at android.app.ActivityThread.-wrap16(ActivityThread.java)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5461)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: java.lang.IllegalArgumentException: width and height must be > 0
W/System.err:     at android.graphics.Bitmap.createBitmap(Bitmap.java:830)
W/System.err:     at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
W/System.err:     at android.graphics.Bitmap.createBitmap(Bitmap.java:776)
W/System.err:     at com.sfproduction.happypark.RegisterActivity$CompressBitmap.compressImage(RegisterActivity.java:810)
W/System.err:     at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:769)
W/System.err:     at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:753)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err:     at java.lang.Thread.run(Thread.java:818)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                  Process: com.sfproduction.happypark, PID: 2494
                  Theme: themes:{}
                  java.lang.RuntimeException: An error occurred while executing doInBackground()
                      at android.os.AsyncTask$3.done(AsyncTask.java:309)
                      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                      at java.lang.Thread.run(Thread.java:818)
                   Caused by: java.lang.IllegalArgumentException: width and height must be > 0
                      at android.graphics.Bitmap.createBitmap(Bitmap.java:830)
                      at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
                      at android.graphics.Bitmap.createBitmap(Bitmap.java:776)
                      at com.sfproduction.happypark.RegisterActivity$CompressBitmap.compressImage(RegisterActivity.java:810)
                      at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:769)
                      at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:753)
                      at android.os.AsyncTask$2.call(AsyncTask.java:295)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                      at java.lang.Thread.run(Thread.java:818) 

如果我使用 InputStream,我会得到 bmp == null

我将所有必需的权限放在 AndroidManifest 中。

如何修复此错误,或使用其他方法进行此压缩?

最佳答案

您正在使用 Picasso!您没有理由自己进行任何“压缩”1!

使用 Picasso 提供的方法为您进行缩放。使用 resize() 方法:

/* Keep in mind #resize() takes the width and height parameters as px values, not dp */
Picasso.with(this).load(newPath).resize(1280, 1280).centerCrop().into(addPhoto, new CallBack() {
    // implementation
});

或者使用 fit() 方法:

Picasso.with(this).load(newPath).fit().into(addPhoto, new Callback() {
    // implementation
});

您可以使用 Target 从 Picasso 获取 Bitmap(此方法仅适用于 resize(),不适用于 适合()):

Picasso.with(this).load(newPath).resize(1280, 1280).into(new Target() {

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        imageView.setImageBitmap(bitmap);
        // TODO: use bitmap for other purposes
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        imageView.setImageDrawable(placeHolderDrawable);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        imageView.setImageDrawable(errorDrawable);
    }

});

要实际压缩 Bitmap,您可以使用ByteArrayOutputStreamBitmap.compress() 方法:

ByteArrayOutputStream out = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

如果你想要无损压缩(图像质量没有损失),将第二个参数设置为 100。否则,你可以将参数设置为 0 到 100 之间的任何值,这将导致文件大小变小,同时损失图像质量。

然后您可以使用此ByteArrayOutputStream 将数据发送到您的服务器。

1 从技术上讲,您尝试做的不是data compression ,您只会缩小图像的比例。不过,有时两者确实协同工作。

关于android - 如何正确压缩此位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39999202/

相关文章:

android - ImageView 使应用程序崩溃

android - 如何获得带有联系人 ID 的联系人更改通知?

java - Appium:无法在android中的datepicker中选择按钮

java - 尽管回收了所有位图并收集垃圾,但在 Activity 之间来回时出现 OutOfMemoryException

ios - 我如何将 UIWebView 转换为位图

android - Bitmap.getPixel() 的替代方法

android - 从内部存储创建和共享文件

java - 如何从非 Activity 和 finish() 获取应用程序上下文;?

android - 如何确保应用程序每次打开时都重新启动,即使它只是放在后台?安卓

java - 如何确定 64 位值中第 n 个最高有效位组的位置?