Creación de un mapa de bits escalado con createScaledBitmap en Android
Quiero crear un mapa de bits a escala, pero aparentemente obtener una imagen desproporcional. Parece un cuadrado mientras que quiero ser rectangular.
Mi código:
- BitmapFactory.Options da 0 ancho y altura
- Cargar imagen grande del servidor en Android
- Android java porcentaje bitmap píxel diferencia entre dos imágenes
- OutOfMemoryError mientras decodifica y codifica Base64 String en Bitmap
- Fuera de memoria en una asignación de bytes (Bitmap como String al servicio web con jabón)
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 960, 960, false);
Quiero que la imagen tenga un MAX de 960. ¿Cómo haría eso? El ancho de configuración a null
no se compila. Es probablemente simple, pero no puedo envolver mi cabeza alrededor de él. Gracias
- Android Bitmpafun se bloquea en BitmapFactory.decodeFileDescriptor
- Android NDK y renderizado para vivir fondos de pantalla
- Rotación de un mapa de bits mediante JNI y NDK
- ¿Hay una solución de reproducción de video más rápida que las funciones de AndroidBitmap_xxx?
- Obtener Bitmap de ImageView cargado con Picasso
- ¿Cómo obtengo el mapa de bits resultante de una imagen después de haber sido redimensionado en una vista de imagen con picasso
- Si se establece myBitmap = null sin recycle (), qué diferencia con recycle ()
- ¿Cómo puedo imprimir una imagen en una impresora Bluetooth en Android?
Si ya tiene el mapa de bits original en la memoria, no necesita hacer todo el proceso de inJustDecodeBounds
, inSampleSize
, etc. Sólo tiene que averiguar qué proporción utilizar y escalar en consecuencia.
final int maxSize = 960; int outWidth; int outHeight; int inWidth = myBitmap.getWidth(); int inHeight = myBitmap.getHeight(); if(inWidth > inHeight){ outWidth = maxSize; outHeight = (inHeight * maxSize) / inWidth; } else { outHeight = maxSize; outWidth = (inWidth * maxSize) / inHeight; } Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);
Si el único uso para esta imagen es una versión escalada, es mejor utilizar la respuesta de Tobiel, para minimizar el uso de memoria.
Tu imagen es cuadrada porque estás estableciendo width = 960
y height = 960
.
Usted necesita crear un método donde usted pasa el tamaño de la imagen que usted quiere como esto: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
En código se ve así:
public static Bitmap lessResolution (String filePath, int width, int height) { int reqHeight = height; int reqWidth = width; BitmapFactory.Options options = new BitmapFactory.Options(); // First decode with inJustDecodeBounds=true to check dimensions options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }
bmpimg = Bitmap.createScaledBitmap(srcimg, 100, 50, true);