<commit_before>package com.sw926.imagefileselector;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUtils {

    public static Bitmap compressImageFile(String srcFile, int maxWidth, int maxHeight) {
        Bitmap bitmap = null;

        long inputFileLength = new File(srcFile).length();
        AppLogger.i(ImageCompressHelper.TAG, "compress file:" + srcFile);
        AppLogger.i(ImageCompressHelper.TAG, "file length:" + (inputFileLength / 1024.0) + "kb");
        AppLogger.i(ImageCompressHelper.TAG, "max output size:(" + maxWidth + "," + maxHeight);

        BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
        decodeOptions.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(srcFile, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;

        AppLogger.i(ImageCompressHelper.TAG, "input size:(" + actualWidth + ", " + actualHeight);

        if (actualHeight <= maxHeight && actualWidth <= maxWidth) {
            AppLogger.w(ImageCompressHelper.TAG, "no need to compress: input size < output size");
            decodeOptions.inJustDecodeBounds = false;
            try {
                bitmap = BitmapFactory.decodeFile(srcFile);
            } catch (OutOfMemoryError e) {
                AppLogger.printStackTrace(e);
                AppLogger.e(ImageCompressHelper.TAG, "OutOfMemoryError:" + srcFile + ", size:" + actualWidth + "," + actualHeight);
            }

            if (bitmap != null) {
                int degree = ImageUtils.getExifOrientation(srcFile);
                if (degree != 0) {
                    AppLogger.w(ImageCompressHelper.TAG, "rotate image from: " + degree);
                    bitmap = rotateImageNotNull(degree, bitmap);
                }
            }
            return bitmap;
        }

        int sampleSize;
        int w;
        int h;
        if (actualWidth * maxHeight > maxWidth * actualHeight) {
            w = maxWidth;
            h = (int) (w * actualHeight / (double) actualWidth);
            sampleSize = (int) (actualWidth / (double) maxWidth);
        } else {
            h = maxHeight;
            w = (int) (h * actualWidth / (double) actualHeight);
            sampleSize = (int) (actualHeight / (double) maxHeight);
        }

        AppLogger.i(ImageCompressHelper.TAG, "in simple size:" + sampleSize);

        decodeOptions.inJustDecodeBounds = false;
        decodeOptions.inSampleSize = sampleSize;
        decodeOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        decodeOptions.inPurgeable = true;
        decodeOptions.inInputShareable = true;
        try {
            bitmap = BitmapFactory.decodeFile(srcFile, decodeOptions);
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
            AppLogger.e(ImageCompressHelper.TAG, "OutOfMemoryError:" + srcFile + ", size:" + actualWidth + "," + actualHeight);
        }

        if (bitmap != null) {
            AppLogger.i(ImageCompressHelper.TAG, "bitmap size after decode:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");

            if (bitmap.getWidth() > maxWidth || bitmap.getHeight() > maxHeight) {
                Bitmap tempBitmap = null;
                try {
                    tempBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
                } catch (OutOfMemoryError e) {
                    AppLogger.printStackTrace(e);
                }
                if (tempBitmap != null) {
                    bitmap.recycle();
                    bitmap = tempBitmap;
                    AppLogger.i(ImageCompressHelper.TAG, "scale down:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");
                }
            }

            int degree = ImageUtils.getExifOrientation(srcFile);
            if (degree != 0) {
                AppLogger.i(ImageCompressHelper.TAG, "rotate image from: " + degree);
                bitmap = rotateImageNotNull(degree, bitmap);
            }
            AppLogger.i(ImageCompressHelper.TAG, "output file width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        }

        return bitmap;
    }


    public static void saveBitmap(Bitmap bmp, String filePath, Bitmap.CompressFormat format, int quality) {
        FileOutputStream fo;
        try {
            File f = new File(filePath);
            if (!f.getParentFile().exists()) {
                //noinspection ResultOfMethodCallIgnored
                f.getParentFile().mkdirs();
            }

            if (f.exists()) {
                //noinspection ResultOfMethodCallIgnored
                f.delete();
            }
            //noinspection ResultOfMethodCallIgnored
            f.createNewFile();
            fo = new FileOutputStream(f, true);
            bmp.compress(format, quality, fo);
            fo.flush();
            fo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }<commit_msg>add annotation<commit_after>package com.sw926.imagefileselector;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUtils {

    @Nullable
    public static Bitmap compressImageFile(@NonNull String srcFile, int maxWidth, int maxHeight) {
        Bitmap bitmap = null;

        long inputFileLength = new File(srcFile).length();
        AppLogger.i(ImageCompressHelper.TAG, "compress file:" + srcFile);
        AppLogger.i(ImageCompressHelper.TAG, "file length:" + (inputFileLength / 1024.0) + "kb");
        AppLogger.i(ImageCompressHelper.TAG, "max output size:(" + maxWidth + "," + maxHeight);

        BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
        decodeOptions.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(srcFile, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;

        AppLogger.i(ImageCompressHelper.TAG, "input size:(" + actualWidth + ", " + actualHeight);

        if (actualHeight <= maxHeight && actualWidth <= maxWidth) {
            AppLogger.w(ImageCompressHelper.TAG, "no need to compress: input size < output size");
            decodeOptions.inJustDecodeBounds = false;
            try {
                bitmap = BitmapFactory.decodeFile(srcFile);
            } catch (OutOfMemoryError e) {
                AppLogger.printStackTrace(e);
                AppLogger.e(ImageCompressHelper.TAG, "OutOfMemoryError:" + srcFile + ", size:" + actualWidth + "," + actualHeight);
            }

            if (bitmap != null) {
                int degree = ImageUtils.getExifOrientation(srcFile);
                if (degree != 0) {
                    AppLogger.w(ImageCompressHelper.TAG, "rotate image from: " + degree);
                    bitmap = rotateImageNotNull(degree, bitmap);
                }
            }
            return bitmap;
        }

        int sampleSize;
        int w;
        int h;
        if (actualWidth * maxHeight > maxWidth * actualHeight) {
            w = maxWidth;
            h = (int) (w * actualHeight / (double) actualWidth);
            sampleSize = (int) (actualWidth / (double) maxWidth);
        } else {
            h = maxHeight;
            w = (int) (h * actualWidth / (double) actualHeight);
            sampleSize = (int) (actualHeight / (double) maxHeight);
        }

        AppLogger.i(ImageCompressHelper.TAG, "in simple size:" + sampleSize);

        decodeOptions.inJustDecodeBounds = false;
        decodeOptions.inSampleSize = sampleSize;
        decodeOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        decodeOptions.inPurgeable = true;
        decodeOptions.inInputShareable = true;
        try {
            bitmap = BitmapFactory.decodeFile(srcFile, decodeOptions);
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
            AppLogger.e(ImageCompressHelper.TAG, "OutOfMemoryError:" + srcFile + ", size:" + actualWidth + "," + actualHeight);
        }

        if (bitmap != null) {
            AppLogger.i(ImageCompressHelper.TAG, "bitmap size after decode:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");

            if (bitmap.getWidth() > maxWidth || bitmap.getHeight() > maxHeight) {
                Bitmap tempBitmap = null;
                try {
                    tempBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
                } catch (OutOfMemoryError e) {
                    AppLogger.printStackTrace(e);
                }
                if (tempBitmap != null) {
                    bitmap.recycle();
                    bitmap = tempBitmap;
                    AppLogger.i(ImageCompressHelper.TAG, "scale down:(" + bitmap.getWidth() + ", " + bitmap.getHeight() + ")");
                }
            }

            int degree = ImageUtils.getExifOrientation(srcFile);
            if (degree != 0) {
                AppLogger.i(ImageCompressHelper.TAG, "rotate image from: " + degree);
                bitmap = rotateImageNotNull(degree, bitmap);
            }
            AppLogger.i(ImageCompressHelper.TAG, "output file width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        }

        return bitmap;
    }

    public static void saveBitmap(Bitmap bmp, String filePath, Bitmap.CompressFormat format, int quality) {
        FileOutputStream fo;
        try {
            File f = new File(filePath);
            if (!f.getParentFile().exists()) {
                //noinspection ResultOfMethodCallIgnored
                f.getParentFile().mkdirs();
            }

            if (f.exists()) {
                //noinspection ResultOfMethodCallIgnored
                f.delete();
            }
            //noinspection ResultOfMethodCallIgnored
            f.createNewFile();
            fo = new FileOutputStream(f, true);
            bmp.compress(format, quality, fo);
            fo.flush();
            fo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
{'fraction_non_alphanumeric': 0.10953112990007687, 'fraction_numerical': 0.002498078401229823, 'mean_word_length': 2.0373504522906334, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2369730', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2683, 'n_words': 589}