如何解决将图像分成多个部分
| 我将图像存储到SD卡中。 我想将图像分成16个相等的部分。 如何使用位图?解决方法
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,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,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();
}
}
,尝试这样的事情:
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 <-您的位图
,我发现下面的代码很棒。它将图像分为9部分。您可以使用此代码将图像分为16个部分。这是一个非常简单的方法。
public Bitmap[] splitBitmap(Bitmap picture)
{
Bitmap scaledBitmap = Bitmap.createScaledBitmap(picture,240,true);
Bitmap[] imgs = new Bitmap[9];
imgs[0] = Bitmap.createBitmap(scaledBitmap,80,80);
imgs[1] = Bitmap.createBitmap(scaledBitmap,80);
imgs[2] = Bitmap.createBitmap(scaledBitmap,160,80);
imgs[3] = Bitmap.createBitmap(scaledBitmap,80);
imgs[4] = Bitmap.createBitmap(scaledBitmap,80);
imgs[5] = Bitmap.createBitmap(scaledBitmap,80);
imgs[6] = Bitmap.createBitmap(scaledBitmap,80);
imgs[7] = Bitmap.createBitmap(scaledBitmap,80);
imgs[8] = Bitmap.createBitmap(scaledBitmap,80);
return imgs;
}
该函数将原始位图作为参数,然后使用Bitmap.createScaledBitmap(picture,240,240,true);我创建了一个尺寸为240 x 240的缩放图像,以将图像分成相等的部分,我创建了一个3 x 3的网格,其中每个图像的尺寸为80 x80。可以根据需要进行更改,但是宽度应保持为240,因为所有正常的android手机屏幕均为240度。
所有位图都存储在位图数组中,最后该函数将数组返回给调用函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。