Cortar una imagen de mapa de bits

¿Cómo puedo recortar una imagen de mapa de bits? Esta es mi pregunta he intentado algunos conceptos usando intentos pero todavía fallan.

Estoy teniendo una imagen de mapa de bits que quiero recortar!

Aquí está el código:

Intent intent = new Intent("com.android.camera.action.CROP"); intent.setClassName("com.android.camera", "com.android.camera.CropImage"); File file = new File(filePath); Uri uri = Uri.fromFile(file); intent.setData(uri); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 96); intent.putExtra("outputY", 96); intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", true); startActivityForResult(intent, REQUEST_CROP_ICON); 

¿Podría alguien ayudarme con respecto a este @ Gracias

He utilizado este método para recortar la imagen y funciona perfectamente:

 Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.xyz); resizedbitmap1=Bitmap.createBitmap(bmp, 0,0,yourwidth, yourheight); 

CreateBitmap () toma bitmap, inicia X, inicia Y, width & height como parámetro

Utilizar la respuesta anterior no funciona si desea cortar / cortar áreas particulares fuera de límites ! Usando este código siempre obtendrá el tamaño deseado – incluso si el origen es más pequeño.

 // Here I want to slice a piece "out of bounds" starting at -50, -25 // Given an endposition of 150, 75 you will get a result of 200x100px Rect rect = new Rect(-50, -25, 150, 75); // Be sure that there is at least 1px to slice. assert(rect.left < rect.right && rect.top < rect.bottom); // Create our resulting image (150--50),(75--25) = 200x100px Bitmap resultBmp = Bitmap.createBitmap(rect.right-rect.left, rect.bottom-rect.top, Bitmap.Config.ARGB_8888); // draw source bitmap into resulting image at given position: new Canvas(resultBmp).drawBitmap(bmp, -rect.left, -rect.top, null); 

…¡y tu estas listo!

Tuve un problema similar con la cosecha y después de intentar numerosos enfoques me di cuenta de este que tenía sentido para mí. Este método sólo cultiva la imagen a la forma cuadrada, todavía estoy trabajando en la forma circular (No dude en modificar el código para obtener la forma que necesita).

Por lo tanto, primero tiene yout mapa de bits que desea recortar:

 Bitmap image; //you need to initialize it in your code first of course 

La información de la imagen se almacena en una matriz int [] lo que no es más que una matriz de números enteros que contiene el valor de color de cada píxel, comenzando en la esquina superior izquierda de la imagen con índice 0 y terminando en la esquina inferior derecha con índice N . Puede obtener esta matriz con el método Bitmap.getPixels () que toma varios argumentos.

Necesitamos la forma cuadrada, por lo tanto necesitamos acortar el más largo de los lados. Además, para mantener la imagen centrada, el recorte debe realizarse en ambos lados de la imagen. Espero que la imagen le ayudará a entender lo que quiero decir. Representación visual del recorte. Los puntos rojos en la imagen representan los píxeles inicial y final que necesitamos y la variable con el guión es numéricamente igual a la misma variable sin el guión.

Ahora finalmente el código:

 int imageHeight = image.getHeight(); //get original image height int imageWidth = image.getWidth(); //get original image width int offset = 0; int shorterSide = imageWidth < imageHeight ? imageWidth : imageHeight; int longerSide = imageWidth < imageHeight ? imageHeight : imageWidth; boolean portrait = imageWidth < imageHeight ? true : false; //find out the image orientation //number array positions to allocate for one row of the pixels (+ some blanks - explained in the Bitmap.getPixels() documentation) int stride = shorterSide + 1; int lengthToCrop = (longerSide - shorterSide) / 2; //number of pixel to remove from each side //size of the array to hold the pixels (amount of pixels) + (amount of strides after every line) int pixelArraySize = (shorterSide * shorterSide) + (shorterImageDimension * 1); int pixels = new int[pixelArraySize]; //now fill the pixels with the selected range image.getPixels(pixels, 0, stride, portrait ? 0 : lengthToCrop, portrait ? lengthToCrop : 0, shorterSide, shorterSide); //save memory image.recycle(); //create new bitmap to contain the cropped pixels Bitmap croppedBitmap = Bitmap.createBitmap(shorterSide, shorterSide, Bitmap.Config.ARGB_4444); croppedBitmap.setPixels(pixels, offset, 0, 0, shorterSide, shorterSide); //I'd recommend to perform these kind of operations on worker thread listener.imageCropped(croppedBitmap); //Or if you like to live dangerously return croppedBitmap; 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.