Reducir el tamaño de un mapa de bits a un tamaño especificado en Android
Quiero reducir el tamaño de un mapa de bits a 200kb exactamente. Obtengo una imagen de la tarjeta SD, la comprimo y la guardo en la tarjeta sd otra vez con un nombre diferente en un directorio diferente. La compresión funciona bien (3 mb como la imagen se comprime a alrededor de 100 kb). Escribí las siguientes líneas de códigos para esto:
String imagefile ="/sdcard/DCIM/100ANDRO/DSC_0530.jpg"; Bitmap bm = ShrinkBitmap(imagefile, 300, 300); //this method compresses the image and saves into a location in sdcard Bitmap ShrinkBitmap(String file, int width, int height){ BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); bmpFactoryOptions.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); if (heightRatio > 1 || widthRatio > 1) { if (heightRatio > widthRatio) { bmpFactoryOptions.inSampleSize = heightRatio; } else { bmpFactoryOptions.inSampleSize = widthRatio; } } bmpFactoryOptions.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] imageInByte = stream.toByteArray(); //this gives the size of the compressed image in kb long lengthbmp = imageInByte.length / 1024; try { bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/compressed_new.jpg")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap; }
- ¿Por qué setImageBitmap no tiene efecto en ImageButton personalizado?
- Android: pre y post-HONEYCOMB montón de uso debido a los mapas de bits?
- Fuera de error de memoria que se ocupa de mapas de bits grandes y el ciclo de vida de actividad de Android
- Cómo: Parcelar un mapa de bits en Android
- Cómo blit () en android?
- Android: colorea debajo del gráfico de líneas
- FPS malo en el juego 2D android / java en dispositivos de alta definición
- Cargar imágenes grandes en mapa de bits?
- Capturado La orientación de la fotografía está cambiando en android
- Tamaño y tamaño de los mapas de bits de Android canvas
- ¿Cómo aplicar LUT de color a imágenes de mapa de bits para efectos de filtro en android?
- Android: Convertir un mapa de bits en un mapa de bits monocromo (1 bit por pixel)
- Cómo calcular el porcentaje de área borrada de un mapa de bits en android?
He encontrado una respuesta que funciona perfectamente para mí:
/** * reduces the size of the image * @param image * @param maxSize * @return */ public Bitmap getResizedBitmap(Bitmap image, int maxSize) { int width = image.getWidth(); int height = image.getHeight(); float bitmapRatio = (float)width / (float) height; if (bitmapRatio > 1) { width = maxSize; height = (int) (width / bitmapRatio); } else { height = maxSize; width = (int) (height * bitmapRatio); } return Bitmap.createScaledBitmap(image, width, height, true); }
Llamando al método:
Bitmap converetdImage = getResizedBitmap(photo, 500);
- La foto es tu mapa de bits