Divide Image into parts

后端 未结 3 639
后悔当初
后悔当初 2020-12-28 11:04

I have images stored into sd card. I want to divide the image into sixteen equal parts. How to do it using bitmap?

相关标签:
3条回答
  • 2020-12-28 11:22

    I found the below code that work greates. It divide image into 9 parts. You can use this code to divide image into 16 parts. This is a very simple approach.

    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;
    
    
    }
    

    The function takes the original bitmap as a parameter, then using the Bitmap.createScaledBitmap(picture, 240, 240, true); i created a scaled image of size 240 x 240 to split the image into equal pieces, i have created a 3 x 3 by grid, in which each image’s size is of 80 x 80. This can be changed as per your needs, but the width should be kept to 240, because all the normal android phone screens are 240 dip wide.

    All the bitmaps are stored in a bitmap array, and finally the function returns the array back to the calling function.

    0 讨论(0)
  • 2020-12-28 11:27

    Give it a try to somthing like this:

    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 <- your bitmaps

    0 讨论(0)
  • 2020-12-28 11:34
    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题