Tengo imágenes almacenadas en la tarjeta sd. Quiero dividir la imagen en dieciséis partes iguales. ¿Cómo hacerlo usando mapa de bits?Divide la imagen en las partes
Respuesta
public class CropImageManipulator
{
public CropImageManipulator()
{
}
private string _fileNameWithoutExtension;
private string _fileExtension;
private string _fileDirectory;
public void Cropping(string inputImgPath, int cropWidth, int cropHeight)
{
this._fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(inputImgPath);
this._fileExtension = System.IO.Path.GetExtension(inputImgPath);
this._fileDirectory = System.IO.Path.GetDirectoryName(inputImgPath);
//Load the image divided
Image inputImg = Image.FromFile(inputImgPath);
int imgWidth = inputImg.Width;
int imgHeight = inputImg.Height;
//Divide how many small blocks
int widthCount = (int)Math.Ceiling((imgWidth * 1.00)/(cropWidth * 1.00));
int heightCount = (int)Math.Ceiling((imgHeight * 1.00)/(cropHeight * 1.00));
ArrayList areaList = new ArrayList();
int i = 0;
for (int iHeight = 0; iHeight < heightCount ; iHeight ++)
{
for (int iWidth = 0; iWidth < widthCount ; iWidth ++)
{
int pointX = iWidth * cropWidth;
int pointY = iHeight * cropHeight;
int areaWidth = ((pointX + cropWidth) > imgWidth) ? (imgWidth - pointX) : cropWidth;
int areaHeight = ((pointY + cropHeight) > imgHeight) ? (imgHeight - pointY) : cropHeight;
string s = string.Format("{0};{1};{2};{3}",pointX,pointY,areaWidth,areaHeight);
Rectangle rect = new Rectangle(pointX,pointY,areaWidth,areaHeight);
areaList.Add(rect);
i ++;
}
}
for (int iLoop = 0 ; iLoop < areaList.Count ; iLoop ++)
{
Rectangle rect = (Rectangle)areaList[iLoop];
string fileName = this._fileDirectory + "\\" + this._fileNameWithoutExtension + "_" + iLoop.ToString() + this._fileExtension;
Bitmap newBmp = new Bitmap(rect.Width,rect.Height,PixelFormat.Format24bppRgb);
Graphics newBmpGraphics = Graphics.FromImage(newBmp);
newBmpGraphics.DrawImage(inputImg,new Rectangle(0,0,rect.Width,rect.Height),rect,GraphicsUnit.Pixel);
newBmpGraphics.Save();
switch (this._fileExtension.ToLower())
{
case ".jpg":
case ".jpeg":
newBmp.Save(fileName,ImageFormat.Jpeg);
break;
case "gif":
newBmp.Save(fileName,ImageFormat.Gif);
break;
}
}
inputImg.Dispose();
}
}
darle una oportunidad a algo como esto:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final int width = dm.widthPixels;
final int height = dm.heightPixels;
final int pixelByCol = width/4;
final int pixelByRow = height/4;
List<Bitmap> bs = new ArrayList<Bitmap>();
Bitmap image = <your photo here>
for (int i = 0; i < 4) {
for (int j = 0; j < 4) {
int startX = pixelByCol * i;
int startY = pixelByRow * j;
Bitmap b = Bitmap.createBitmap(image, startX, startY, pixelByCol, pixelByRow);
bs.add(b);
}
}
B < - mapas de bits
he encontrado el siguiente código que funciona greates. Divide la imagen en 9 partes. Puede usar este código para dividir la imagen en 16 partes. Este es un enfoque muy simple.
public Bitmap[] splitBitmap(Bitmap picture)
{
Bitmap scaledBitmap = Bitmap.createScaledBitmap(picture, 240, 240, true);
Bitmap[] imgs = new Bitmap[9];
imgs[0] = Bitmap.createBitmap(scaledBitmap, 0, 0, 80 , 80);
imgs[1] = Bitmap.createBitmap(scaledBitmap, 80, 0, 80, 80);
imgs[2] = Bitmap.createBitmap(scaledBitmap,160, 0, 80,80);
imgs[3] = Bitmap.createBitmap(scaledBitmap, 0, 80, 80, 80);
imgs[4] = Bitmap.createBitmap(scaledBitmap, 80, 80, 80,80);
imgs[5] = Bitmap.createBitmap(scaledBitmap, 160, 80,80,80);
imgs[6] = Bitmap.createBitmap(scaledBitmap, 0, 160, 80,80);
imgs[7] = Bitmap.createBitmap(scaledBitmap, 80, 160,80,80);
imgs[8] = Bitmap.createBitmap(scaledBitmap, 160,160,80,80);
return imgs;
}
la función toma el mapa de bits original como un parámetro, y luego usando el Bitmap.createScaledBitmap (imagen, 240, 240, true); Creé una imagen a escala de tamaño 240 x 240 para dividir la imagen en partes iguales, he creado una cuadrícula de 3 x 3 por cuadrícula, en la que el tamaño de cada imagen es de 80 x 80. Esto se puede cambiar según sus necesidades, pero el el ancho debe mantenerse en 240, ya que todas las pantallas normales del teléfono Android tienen una anchura de 240 pulgadas.
Todos los mapas de bits se almacenan en una matriz de mapa de bits, y finalmente la función devuelve la matriz a la función de llamada.
- 1. divide el conjunto en partes más pequeñas
- 2. ¿Cómo se divide una matriz en 2 partes para que las dos partes tengan el mismo promedio?
- 3. Revelando partes de una imagen al mouseover
- 4. ¿Cómo se divide (fragmento) una matriz de Ruby en partes de elementos X?
- 5. Borde CSS en imagen PNG con partes transparentes
- 6. las partes internas de System.String
- 7. ¿Cómo se divide NSString en componentes?
- 8. Programación multinúcleo: las partes duras
- 9. iOS: Cómo recortar una imagen en las partes útiles (eliminar el borde transparente)
- 10. Javascript divide
- 11. Cambiar las partes de las palabras de CamelCase en vim
- 12. Examinando las partes internas de las funciones en Haskell
- 13. Python divide url para encontrar el nombre de la imagen y la extensión
- 14. ¿Se divide en dos variables?
- 15. ¿Cómo destaco las partes de un mapa de imagen al pasar el mouse?
- 16. conexión fluida entre las partes a trozos
- 17. Documentos para las partes internas de la implementación de CPython
- 18. Abre TIF enorme en .NET y copia partes en la nueva imagen
- 19. Python eliminar las partes de la cadena por el índice
- 20. Colocar la imagen en las coordenadas Android
- 21. Python divide delimitadores consecutivos
- 22. ¿Cuál debería ser la técnica de umbralización ideal para mejorar partes de la imagen?
- 23. Java divide el camino ...?
- 24. php divide a preg_split
- 25. ¿Cómo divide strtok() la cadena en tokens en C?
- 26. Python: Pandas Divide DataFrame en la primera fila
- 27. Cortar un intervalo de tiempo en las partes
- 28. ¿Cómo puedo configurar las partes web configurables en Sharepoint?
- 29. JavaScript: las partes buenas - ¿Cómo no usar `new` en absoluto
- 30. C#: Guardando archivos JPG en partes
A partir del nivel de API 10 puede hacerlo de manera eficiente utilizando BitmapRegionDecoder. – Zelimir
Pero estoy usando el nivel api 4. Por favor, ayúdenme –
Entonces, no conozco ninguna manera más inteligente que usar bucle pasando más de 2 coordenadas (0 hasta Ancho, 0 hasta altura) y poner partes de Bitmap en bitmaps separados. – Zelimir