我將圖像存儲到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