2019-03-03 09:29:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Hemanth Savarala.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
package code.name.monkey.retromusic.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
2020-10-06 08:46:04 +00:00
|
|
|
* Created on : June 18, 2016 Author : zetbaitsu Name : Zetra GitHub : https://github.com/zetbaitsu
|
2018-07-27 13:07:33 +00:00
|
|
|
*/
|
|
|
|
public class Compressor {
|
2020-10-06 08:46:04 +00:00
|
|
|
// max width and height values of the compressed image is taken as 612x816
|
|
|
|
private int maxWidth = 612;
|
|
|
|
private int maxHeight = 816;
|
|
|
|
private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
|
|
|
|
private int quality = 80;
|
|
|
|
private String destinationDirectoryPath;
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public Compressor(Context context) {
|
|
|
|
destinationDirectoryPath = context.getCacheDir().getPath() + File.separator + "images";
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public Compressor setMaxWidth(int maxWidth) {
|
|
|
|
this.maxWidth = maxWidth;
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public Compressor setMaxHeight(int maxHeight) {
|
|
|
|
this.maxHeight = maxHeight;
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public Compressor setCompressFormat(Bitmap.CompressFormat compressFormat) {
|
|
|
|
this.compressFormat = compressFormat;
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public Compressor setQuality(int quality) {
|
|
|
|
this.quality = quality;
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public Compressor setDestinationDirectoryPath(String destinationDirectoryPath) {
|
|
|
|
this.destinationDirectoryPath = destinationDirectoryPath;
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public File compressToFile(File imageFile) throws IOException {
|
|
|
|
return compressToFile(imageFile, imageFile.getName());
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public File compressToFile(File imageFile, String compressedFileName) throws IOException {
|
|
|
|
return ImageUtil.compressImage(
|
|
|
|
imageFile,
|
|
|
|
maxWidth,
|
|
|
|
maxHeight,
|
|
|
|
compressFormat,
|
|
|
|
quality,
|
|
|
|
destinationDirectoryPath + File.separator + compressedFileName);
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public Bitmap compressToBitmap(File imageFile) throws IOException {
|
|
|
|
return ImageUtil.decodeSampledBitmapFromFile(imageFile, maxWidth, maxHeight);
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|