我将图像存储到SD卡中。 我想将图像分成十六个相等的部分。 如何使用位图来做到这一点?将图像分为几部分
Q
将图像分为几部分
5
A
回答
5
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();
}
}
1
给它一个尝试财产以后这样的:
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);
}
}
BS < - 你的位图
0
我发现下面的代码工作的greates。它将图像分成9个部分。您可以使用此代码将图像分成16个部分。这是一个非常简单的方法。
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;
}
该函数采用原始位图作为参数,然后使用Bitmap.createScaledBitmap(图片,240,240,TRUE);我创建了一个尺寸为240 x 240的缩放图像,以将图像分成相等的部分,我创建了一个3 x 3的网格,其中每个图像的尺寸为80 x 80.这可以根据您的需要进行更改,但是宽度应该保持为240,因为所有正常的Android手机屏幕都是240宽的。
所有位图都存储在位图数组中,最后该函数将数组返回给调用函数。
相关问题
- 1. CSS3多列将图像分为几个部分
- 2. 将列表分为几部分
- 3. 将`UITableViewCell`分成几部分
- 4. 如何将图像分成几个部分? Tilemap
- 5. HeightFIeld分为几部分 - PhysX
- 6. 将Rails部分渲染为图像
- 7. 将图像转换为html部分
- 8. 如何将图像分成2部分?
- 9. 将一个字节分成几部分
- 10. 将字符串分成几部分
- 11. QFile:将文件分成几部分
- 12. 将图像划分为不同的部分
- 13. java将图像拆分为2部分失去质量
- 14. 如何将图像分割为多个部分
- 15. 如何在Python中将图像拆分为多个部分
- 16. 将直方图分为三部分
- 17. 将类图分为两部分 - UML
- 18. 将图像拆分为块
- 19. 将图像分解为字符图像
- 20. 将一行Python代码划分为几部分
- 21. 如何将全名分为几个部分
- 22. 在FullCalendar中将天数划分为几个部分
- 23. 如何将Play应用分为几个部分
- 24. 正则表达式将网址分解为几部分
- 25. 将风向分为几类
- 26. 将功能分为几行
- 27. 将我的直方图分段为不同颜色的几个部分R
- 28. 将视图拆分为三部分(不是分割视图)
- 29. 将UIScrollView内部的图像分成若干部分
- 30. 拆分数据分成几个部分
从API级别10开始,您可以使用BitmapRegionDecoder高效地完成此操作。 – Zelimir 2011-04-13 14:28:42
但是我正在使用API级别4.请帮助我 – 2011-04-14 09:45:46
然后我不知道任何比使用循环遍历2个坐标(0到宽度,0到高度)更明智的方法,并将位图部分放到不同的位图中。 – Zelimir 2011-04-14 10:07:56