【问题标题】:Divide Image into parts将图像分成几部分
【发布时间】:2013-08-03 18:57:04
【问题描述】:

我将图像存储在 SD 卡中。 我想把图像分成十六等份。 用位图怎么做?

【问题讨论】:

  • 从 API 级别 10 开始,您可以使用 BitmapRegionDecoder 有效地做到这一点。
  • 但我使用的是 api 级别 4。请帮助我
  • 那么我不知道有什么比使用循环遍历 2 个坐标(0 到宽度,0 到高度)并将位图部分放入单独的位图中更聪明的方法了。
  • 你可以看this link 对Android中的图像分割有一个简单的了解。

标签: android


【解决方案1】:
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();
    }
}

【讨论】:

    【解决方案2】:

    试一试这样的事情:

    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

    【讨论】:

      【解决方案3】:

      我发现下面的代码很好用。它将图像分为 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(picture, 240, 240, true);我创建了一个大小为 240 x 240 的缩放图像以将图像分成相等的部分,我创建了一个 3 x 3 的网格,其中每个图像的大小为 80 x 80。这可以根据您的需要进行更改,但是宽度应保持在 240,因为所有普通的 android 手机屏幕都是 240 dip 宽度。

      所有的位图都存储在一个位图数组中,最后函数将数组返回给调用函数。

      【讨论】:

        猜你喜欢
        • 2014-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-05
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多