android 彩信 图片大小,Android
//最大宽度 :widthLimit 最大高度:heightLimit 最大size:byteLimit
private byte[] getResizedImageData(int widthLimit, int heightLimit, int byteLimit) {
int outWidth = mWidth;//图片本身宽度
int outHeight = mHeight;//图片本身高度
float scaleFactor = 1.F;//压缩比例
while ((outWidth * scaleFactor > widthLimit) || (outHeight * scaleFactor > heightLimit)) {
scaleFactor *= .75F;//一直改变压缩比例,直到确定压缩后的宽度和高度比限制小为止
}
InputStream input = null;
try {
ByteArrayOutputStream os = null;
int attempts = 1;
int sampleSize = 1;
BitmapFactory.Options options = new BitmapFactory.Options();
int quality = MessageUtils.IMAGE_COMPRESSION_QUALITY;
Bitmap b = null;
// In this loop, attempt to decode the stream with the best possible subsampling (we
// start with 1, which means no subsampling - get the original content) without running
// out of memory.
do {
input = mContext.getContentResolver().openInputStream(mUri);
options.inSampleSize = sampleSize;
try {
b = BitmapFactory.decodeStream(input, null, options);
} catch (OutOfMemoryError e) {
Log.w(TAG, "getResizedImageData: img too large to decode (OutOfMemoryError), " +
"may try with larger sampleSize. Curr sampleSize=" + sampleSize);
sampleSize *= 2; // works best as a power of two
attempts++;
continue;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
} while (b == null && attempts < NUMBER_OF_RESIZE_ATTEMPTS);
if (b == null) {
return null;
}
attempts = 1; // reset count for second loop
// In this loop, we attempt to compress/resize the content to fit the given dimension
// and file-size limits.
do {
try {
if (options.outWidth > widthLimit || options.outHeight > heightLimit ||
(os != null && os.size() > byteLimit)) {
// The decoder does not support the inSampleSize option.
// Scale the bitmap using Bitmap library.
int scaledWidth = (int)(outWidth * scaleFactor);
int scaledHeight = (int)(outHeight * scaleFactor);
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
Log.v(TAG, "getResizedImageData: retry scaling using " +
"Bitmap.createScaledBitmap: w=" + scaledWidth +
", h=" + scaledHeight);
}
b = Bitmap.createScaledBitmap(b, scaledWidth, scaledHeight, false);
if (b == null) {
return null;
}
}
// Compress the image into a JPG. Start with MessageUtils.IMAGE_COMPRESSION_QUALITY.
// In case that the image byte size is still too large reduce the quality in
// proportion to the desired byte size.
os = new ByteArrayOutputStream();
bpress(CompressFormat.JPEG, quality, os);
int jpgFileSize = os.size();
if (jpgFileSize > byteLimit) {//如果压缩后的大小比限制大,就改变quality再进行压缩(或者压缩次数达到,不能再压缩)
quality = (quality * byteLimit) / jpgFileSize; // watch for int division!
if (quality < MessageUtils.MINIMUM_IMAGE_COMPRESSION_QUALITY) {
quality = MessageUtils.MINIMUM_IMAGE_COMPRESSION_QUALITY;//quality有最小值限制
}
os = new ByteArrayOutputStream();
bpress(CompressFormat.JPEG, quality, os);
}
} catch (java.lang.OutOfMemoryError e) {}
scaleFactor *= .75F;
attempts++;
} while ((os == null || os.size() > byteLimit) && attempts < NUMBER_OF_RESIZE_ATTEMPTS);
b.recycle(); // done with the bitmap, release the memory
return os == null ? null : os.toByteArray();
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage(), e);
return null;
} catch (java.lang.OutOfMemoryError e) {
Log.e(TAG, e.getMessage(), e);
return null;
}
}
android 彩信 图片大小,Android
//最大宽度 :widthLimit 最大高度:heightLimit 最大size:byteLimit
private byte[] getResizedImageData(int widthLimit, int heightLimit, int byteLimit) {
int outWidth = mWidth;//图片本身宽度
int outHeight = mHeight;//图片本身高度
float scaleFactor = 1.F;//压缩比例
while ((outWidth * scaleFactor > widthLimit) || (outHeight * scaleFactor > heightLimit)) {
scaleFactor *= .75F;//一直改变压缩比例,直到确定压缩后的宽度和高度比限制小为止
}
InputStream input = null;
try {
ByteArrayOutputStream os = null;
int attempts = 1;
int sampleSize = 1;
BitmapFactory.Options options = new BitmapFactory.Options();
int quality = MessageUtils.IMAGE_COMPRESSION_QUALITY;
Bitmap b = null;
// In this loop, attempt to decode the stream with the best possible subsampling (we
// start with 1, which means no subsampling - get the original content) without running
// out of memory.
do {
input = mContext.getContentResolver().openInputStream(mUri);
options.inSampleSize = sampleSize;
try {
b = BitmapFactory.decodeStream(input, null, options);
} catch (OutOfMemoryError e) {
Log.w(TAG, "getResizedImageData: img too large to decode (OutOfMemoryError), " +
"may try with larger sampleSize. Curr sampleSize=" + sampleSize);
sampleSize *= 2; // works best as a power of two
attempts++;
continue;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
} while (b == null && attempts < NUMBER_OF_RESIZE_ATTEMPTS);
if (b == null) {
return null;
}
attempts = 1; // reset count for second loop
// In this loop, we attempt to compress/resize the content to fit the given dimension
// and file-size limits.
do {
try {
if (options.outWidth > widthLimit || options.outHeight > heightLimit ||
(os != null && os.size() > byteLimit)) {
// The decoder does not support the inSampleSize option.
// Scale the bitmap using Bitmap library.
int scaledWidth = (int)(outWidth * scaleFactor);
int scaledHeight = (int)(outHeight * scaleFactor);
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
Log.v(TAG, "getResizedImageData: retry scaling using " +
"Bitmap.createScaledBitmap: w=" + scaledWidth +
", h=" + scaledHeight);
}
b = Bitmap.createScaledBitmap(b, scaledWidth, scaledHeight, false);
if (b == null) {
return null;
}
}
// Compress the image into a JPG. Start with MessageUtils.IMAGE_COMPRESSION_QUALITY.
// In case that the image byte size is still too large reduce the quality in
// proportion to the desired byte size.
os = new ByteArrayOutputStream();
bpress(CompressFormat.JPEG, quality, os);
int jpgFileSize = os.size();
if (jpgFileSize > byteLimit) {//如果压缩后的大小比限制大,就改变quality再进行压缩(或者压缩次数达到,不能再压缩)
quality = (quality * byteLimit) / jpgFileSize; // watch for int division!
if (quality < MessageUtils.MINIMUM_IMAGE_COMPRESSION_QUALITY) {
quality = MessageUtils.MINIMUM_IMAGE_COMPRESSION_QUALITY;//quality有最小值限制
}
os = new ByteArrayOutputStream();
bpress(CompressFormat.JPEG, quality, os);
}
} catch (java.lang.OutOfMemoryError e) {}
scaleFactor *= .75F;
attempts++;
} while ((os == null || os.size() > byteLimit) && attempts < NUMBER_OF_RESIZE_ATTEMPTS);
b.recycle(); // done with the bitmap, release the memory
return os == null ? null : os.toByteArray();
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage(), e);
return null;
} catch (java.lang.OutOfMemoryError e) {
Log.e(TAG, e.getMessage(), e);
return null;
}
}