2011-03-08 15 views
5

He estado intentando esto por algún tiempo, me gustaría crear un fondo de pantalla desde Bitmap. Digamos que el tamaño de fondo de pantalla deseado es 320x480, y el tamaño de la imagen de origen es 2048x2048.Recortar imagen en Android

No estoy seguro de si recortar o ajustar es el término correcto, pero lo que me gustaría lograr es conseguir que la mayor parte de la imagen tenga la misma proporción que el tamaño de fondo deseado (320x480).

Entonces, en este caso, me gustaría obtener 2048x1365 o (1365.333 ... para ser exactos) de la fuente Bitmap, y reducirla a 320x480.

La técnica que he tratado es:

1) recortar la mapa de bits en 2048x1365 primera

bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365); 

2) Reduce la escala a 320x480

bm = Bitmap.createScaledBitmap(bm, 320, 480, false); 

que produjo error OutOfMemory.

¿Hay alguna manera de lograr esto?

Saludos,

dezull

+0

creo que el título sería mejor descrito como 'escala-a-ajuste, manteniendo misma relación de aspecto' –

+0

Gracias, que puede ser que también en forma como el título, pero en realidad, lo que quería para lograr es 'escalar' y 'recortar' un área de la imagen para que se ajuste a – dezull

+0

si la resolvió, por favor comparta su solución. – Tushar

Respuesta

15

Gracias al código abierto, he encontrado la respuesta de la Galería de Android código fuente here en línea 230 :-D

croppedImage = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565); 
Canvas canvas = new Canvas(croppedImage); 

Rect srcRect = mCrop.getCropRect(); 
Rect dstRect = new Rect(0, 0, mOutputX, mOutputY); 

int dx = (srcRect.width() - dstRect.width())/2; 
int dy = (srcRect.height() - dstRect.height())/2; 

// If the srcRect is too big, use the center part of it. 
srcRect.inset(Math.max(0, dx), Math.max(0, dy)); 

// If the dstRect is too big, use the center part of it. 
dstRect.inset(Math.max(0, -dx), Math.max(0, -dy)); 

// Draw the cropped bitmap in the center 
canvas.drawBitmap(mBitmap, srcRect, dstRect, null); 
+3

Tengo el mismo problema. ¿Podrías explicar qué es mOutputX, mCrop ... o mejor? ¿Podrías escribir y realizar un ejemplo de función que recibir el mapa de bits y devolver el mapa de bits recortado y escalado? Muchas gracias. – Ton

+0

Esos son el ancho y la altura de salida. Este tema exacto se ha agregado al sitio de desarrolladores de Android http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – dezull

+0

El enlace ya no funciona. – Warpzit

0

aquí es una respuesta que le consigue que la mayor parte del camino: How to crop an image in android?

+0

Gracias, pero no la solución que estaba buscando. Vea mi propia respuesta, solo use Rect para escalarlo. Y no produce OOM :-) – dezull

9

Sé que esta es una respuesta increíblemente tardía, pero algo g como esto quizá:

public static Bitmap scaleCropToFit(Bitmap original, int targetWidth, int targetHeight){ 
    //Need to scale the image, keeping the aspect ration first 
    int width = original.getWidth(); 
    int height = original.getHeight(); 

    float widthScale = (float) targetWidth/(float) width; 
    float heightScale = (float) targetHeight/(float) height; 
    float scaledWidth; 
    float scaledHeight; 

    int startY = 0; 
    int startX = 0; 

    if (widthScale > heightScale) { 
     scaledWidth = targetWidth; 
     scaledHeight = height * widthScale; 
     //crop height by... 
     startY = (int) ((scaledHeight - targetHeight)/2); 
    } else { 
     scaledHeight = targetHeight; 
     scaledWidth = width * heightScale; 
     //crop width by.. 
     startX = (int) ((scaledWidth - targetWidth)/2); 
    } 

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(original, (int) scaledWidth, (int) scaledHeight, true); 

    Bitmap resizedBitmap = Bitmap.createBitmap(scaledBitmap, startX, startY, targetWidth, targetHeight); 
    return resizedBitmap; 
} 
Cuestiones relacionadas