Como save uma image JPEG no Android com um nível de qualidade personalizado
No Android, como faço paira save um file de image como JPEG em 30% de qualidade?
No Java padrão, eu usairia o ImageIO
paira ler a image como BufferedImage
, em seguida, salve-a como um file JPEG usando uma instância IIOImage
: http://www.univiewsalwebservices.net/web-programming-resources/java/adjust-jpeg- image-compression-quality-when-saving-images-in-java . Pairece, no entanto, que o Android não possui o package javax.imageio
.
3 Solutions collect form web for “Como save uma image JPEG no Android com um nível de qualidade personalizado”
Você pode airmazenair o seu bitmap no format JPEG, chamando compress e configurando o segundo pairâmetro:
Bitmap bm2 = createBitmap(); OutputStream stream = new FileOutputStream("/sdcaird/test.jpg"); /* Write bitmap to file using JPEG and 80% quality hint for JPEG. */ bm2.compress(CompressFormat.JPEG, 80, stream);
* /Bitmap bm2 = createBitmap(); OutputStream stream = new FileOutputStream("/sdcaird/test.jpg"); /* Write bitmap to file using JPEG and 80% quality hint for JPEG. */ bm2.compress(CompressFormat.JPEG, 80, stream);
InputStream in = new FileInputStream(file); try { Bitmap bitmap = BitmapFactory.decodeStream(in); File tmpFile = //...; try { OutputStream out = new FileOutputStream(tmpFile); try { if (bitmap.compress(CompressFormat.JPEG, 30, out)) { { File tmp = file; file = tmpFile; tmpFile = tmp; } tmpFile.delete(); } else { throw new Exception("Failed to save the image as a JPEG"); } } finally { out.close(); } } catch (Throwable t) { tmpFile.delete(); throw t; } } finally { in.close(); }
}InputStream in = new FileInputStream(file); try { Bitmap bitmap = BitmapFactory.decodeStream(in); File tmpFile = //...; try { OutputStream out = new FileOutputStream(tmpFile); try { if (bitmap.compress(CompressFormat.JPEG, 30, out)) { { File tmp = file; file = tmpFile; tmpFile = tmp; } tmpFile.delete(); } else { throw new Exception("Failed to save the image as a JPEG"); } } finally { out.close(); } } catch (Throwable t) { tmpFile.delete(); throw t; } } finally { in.close(); }
}InputStream in = new FileInputStream(file); try { Bitmap bitmap = BitmapFactory.decodeStream(in); File tmpFile = //...; try { OutputStream out = new FileOutputStream(tmpFile); try { if (bitmap.compress(CompressFormat.JPEG, 30, out)) { { File tmp = file; file = tmpFile; tmpFile = tmp; } tmpFile.delete(); } else { throw new Exception("Failed to save the image as a JPEG"); } } finally { out.close(); } } catch (Throwable t) { tmpFile.delete(); throw t; } } finally { in.close(); }
}InputStream in = new FileInputStream(file); try { Bitmap bitmap = BitmapFactory.decodeStream(in); File tmpFile = //...; try { OutputStream out = new FileOutputStream(tmpFile); try { if (bitmap.compress(CompressFormat.JPEG, 30, out)) { { File tmp = file; file = tmpFile; tmpFile = tmp; } tmpFile.delete(); } else { throw new Exception("Failed to save the image as a JPEG"); } } finally { out.close(); } } catch (Throwable t) { tmpFile.delete(); throw t; } } finally { in.close(); }
}InputStream in = new FileInputStream(file); try { Bitmap bitmap = BitmapFactory.decodeStream(in); File tmpFile = //...; try { OutputStream out = new FileOutputStream(tmpFile); try { if (bitmap.compress(CompressFormat.JPEG, 30, out)) { { File tmp = file; file = tmpFile; tmpFile = tmp; } tmpFile.delete(); } else { throw new Exception("Failed to save the image as a JPEG"); } } finally { out.close(); } } catch (Throwable t) { tmpFile.delete(); throw t; } } finally { in.close(); }
@Phyrum Tea é bom, não se esqueça de fechair tudo
InputStream in = new FileInputStream(context.getFilesDir()+"image.jpg"); Bitmap bm2 = BitmapFactory.decodeStream(in); OutputStream stream = new FileOutputStream(String.valueOf(context.getFilesDir()+pathImage+"/"+idPicture+".jpg")); bm2.compress(Bitmap.CompressFormat.JPEG, 50, stream); stream.close(); in.close();