下面列出了android.graphics.Bitmap.CompressFormat#PNG 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public RequestSettings() {
this.emailAddresses = null;
this.emailSubject = "No Subject";
this.emailPrecontent = "";
this.saveLocation = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.icon_request";
this.saveLocation2 = this.saveLocation + "/files";
this.appfilterName = "appfilter.xml";
this.compressFormat = CompressFormat.PNG;
this.appendInformation = true;
this.createAppfilter = true;
this.createZip = true;
this.filterAutomatic = true;
this.filterDefined = true;
this.byteBuffer = 2048;
this.compressQuality = 100;
}
/**
* Creates a RequestSettings Builder object for easily
* assigning custom settings.
*/
public Builder() {
this.emailAddresses = new ArrayList<String>();
this.emailSubject = "No Subject";
this.emailPrecontent = "";
this.saveLocation = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.icon_request";
this.saveLocation2 = this.saveLocation + "/files";
this.appfilterName = "appfilter.xml";
this.compressFormat = CompressFormat.PNG;
this.appendInformation = true;
this.createAppfilter = true;
this.createZip = true;
this.filterAutomatic = true;
this.filterDefined = true;
this.byteBuffer = 2048;
this.compressQuality = 100;
}
public static @NonNull CompressFormat getCompressFormatForContentType(@Nullable String contentType) {
if (contentType == null) return CompressFormat.JPEG;
switch (contentType) {
case MediaUtil.IMAGE_JPEG: return CompressFormat.JPEG;
case MediaUtil.IMAGE_PNG: return CompressFormat.PNG;
case MediaUtil.IMAGE_WEBP: return CompressFormat.WEBP;
default: return CompressFormat.JPEG;
}
}
@Override
public void fillStreamAndClose(NativeBitmap nativebitmap, ImageType type, double quality, OutputStream out) throws IOException {
Bitmap bitmap = ((AndroidBitmap) nativebitmap).bitmap;
CompressFormat compress = type == ImageType.JPEG ? CompressFormat.JPEG : CompressFormat.PNG;
try {
if (!bitmap.compress(compress, (int) (quality * 100), out))
throw new IOException("Unable to compress image with type " + type.name());
} finally {
closeR(out);
}
}
/**
* 判断图片类型
*
* @param b
* @return
*/
public CompressFormat getImageType(byte[] b) {
if (b[1] == (byte) 'P' && b[2] == (byte) 'N' && b[3] == (byte) 'G') {
return CompressFormat.PNG;
} else if (b[6] == (byte) 'J' && b[7] == (byte) 'F' && b[8] == (byte) 'I' && b[9] == (byte) 'F') {
return CompressFormat.JPEG;
} else {
return CompressFormat.JPEG;
}
}
/**
* 通过url或文件名来判断图片类型
*
* @param url
* @return
*/
public static CompressFormat getImageType(String url) {
String name = url.substring(url.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("png")) {
return CompressFormat.PNG;
} else if (name.equalsIgnoreCase("jpg")) {
return CompressFormat.JPEG;
} else {
return CompressFormat.JPEG;
}
}
/**
* 根据文件类型获得CompressFormat.
*
* @Description:
* @Date 2014-3-7
*/
private CompressFormat getCompressFormat(String url) {
String lowerUrl = url.toLowerCase(Locale.ENGLISH);
if (lowerUrl.endsWith(".jpg")) {
return CompressFormat.JPEG;
} else if (lowerUrl.endsWith(".png")) {
return CompressFormat.PNG;
}
return CompressFormat.JPEG;
}
protected static CompressFormat convertExtensionToCompressFormat(String extension) {
return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG;
}
protected static CompressFormat convertExtensionToCompressFormat(String extension) {
return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG;
}
public static <T> byte[] createScaledBytes(Context context, T model, MediaConstraints constraints)
throws BitmapDecodingException
{
CompressFormat compressFormat = CompressFormat.PNG;
int quality = 100;
int attempts = 0;
byte[] bytes;
Bitmap scaledBitmap = Downsampler.AT_MOST.decode(getInputStreamForModel(context, model),
Glide.get(context).getBitmapPool(),
constraints.getImageMaxWidth(context),
constraints.getImageMaxHeight(context),
DecodeFormat.PREFER_RGB_565);
if (scaledBitmap == null) {
throw new BitmapDecodingException("Unable to decode image");
}
try {
do {
attempts++;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
scaledBitmap.compress(compressFormat, quality, baos);
bytes = baos.toByteArray();
Log.w(TAG, "iteration with quality " + quality + "; size " + (bytes.length / 1024) + "kb; attempts " + attempts);
compressFormat = CompressFormat.JPEG;
if (quality > MAX_COMPRESSION_QUALITY) {
quality = MAX_COMPRESSION_QUALITY;
} else {
quality = quality - COMPRESSION_QUALITY_DECREASE;
}
}
while (bytes.length > constraints.getImageMaxSize(context));
return bytes;
} finally {
if (scaledBitmap != null) scaledBitmap.recycle();
}
}
protected static CompressFormat convertExtensionToCompressFormat(String extension) {
return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG;
}