snipt

Ctrl+h for KB shortcuts

Java

Split an image

    public BufferedImage[] splitImage(BufferedImage img, int cols, int rows) 
    {  
        int w = img.getWidth() / cols;  
        int h = img.getHeight() / rows; 
        int num = 0; 
        
        BufferedImage imgs[] = new BufferedImage[cols * rows];  
        
        for( int y = 0; y < rows; y++) 
        {  
            for(int x = 0; x < cols; x++) 
            {  
                imgs[num] = new BufferedImage(w, h, img.getType());   
                Graphics2D g = imgs[num].createGraphics();  
                g.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);  
                g.dispose();  
                num++;  
            }  
        }  
        return imgs; 
https://snipt.net/embed/c5d999e2d29507b8af72ea07270cfcca/
/raw/c5d999e2d29507b8af72ea07270cfcca/
c5d999e2d29507b8af72ea07270cfcca
java
Java
20
2019-07-15T18:13:34
True
False
False
/api/public/snipt/24605/
split-an-image
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a> <a href="#L-19">19</a> <a href="#L-20">20</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a> <span class="kd">public</span> <span class="n">BufferedImage</span><span class="o">[]</span> <span class="nf">splitImage</span><span class="o">(</span><span class="n">BufferedImage</span> <span class="n">img</span><span class="o">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="o">,</span> <span class="kt">int</span> <span class="n">rows</span><span class="o">)</span> </span><span id="L-2"><a name="L-2"></a> <span class="o">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="kt">int</span> <span class="n">w</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="na">getWidth</span><span class="o">()</span> <span class="o">/</span> <span class="n">cols</span><span class="o">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="kt">int</span> <span class="n">h</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="na">getHeight</span><span class="o">()</span> <span class="o">/</span> <span class="n">rows</span><span class="o">;</span> </span><span id="L-5"><a name="L-5"></a> <span class="kt">int</span> <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a> <span class="n">BufferedImage</span> <span class="n">imgs</span><span class="o">[]</span> <span class="o">=</span> <span class="k">new</span> <span class="n">BufferedImage</span><span class="o">[</span><span class="n">cols</span> <span class="o">*</span> <span class="n">rows</span><span class="o">];</span> </span><span id="L-8"><a name="L-8"></a> </span><span id="L-9"><a name="L-9"></a> <span class="k">for</span><span class="o">(</span> <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="n">rows</span><span class="o">;</span> <span class="n">y</span><span class="o">++)</span> </span><span id="L-10"><a name="L-10"></a> <span class="o">{</span> </span><span id="L-11"><a name="L-11"></a> <span class="k">for</span><span class="o">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">cols</span><span class="o">;</span> <span class="n">x</span><span class="o">++)</span> </span><span id="L-12"><a name="L-12"></a> <span class="o">{</span> </span><span id="L-13"><a name="L-13"></a> <span class="n">imgs</span><span class="o">[</span><span class="n">num</span><span class="o">]</span> <span class="o">=</span> <span class="k">new</span> <span class="n">BufferedImage</span><span class="o">(</span><span class="n">w</span><span class="o">,</span> <span class="n">h</span><span class="o">,</span> <span class="n">img</span><span class="o">.</span><span class="na">getType</span><span class="o">());</span> </span><span id="L-14"><a name="L-14"></a> <span class="n">Graphics2D</span> <span class="n">g</span> <span class="o">=</span> <span class="n">imgs</span><span class="o">[</span><span class="n">num</span><span class="o">].</span><span class="na">createGraphics</span><span class="o">();</span> </span><span id="L-15"><a name="L-15"></a> <span class="n">g</span><span class="o">.</span><span class="na">drawImage</span><span class="o">(</span><span class="n">img</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">w</span><span class="o">,</span> <span class="n">h</span><span class="o">,</span> <span class="n">w</span><span class="o">*</span><span class="n">x</span><span class="o">,</span> <span class="n">h</span><span class="o">*</span><span class="n">y</span><span class="o">,</span> <span class="n">w</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="n">w</span><span class="o">,</span> <span class="n">h</span><span class="o">*</span><span class="n">y</span><span class="o">+</span><span class="n">h</span><span class="o">,</span> <span class="kc">null</span><span class="o">);</span> </span><span id="L-16"><a name="L-16"></a> <span class="n">g</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span> </span><span id="L-17"><a name="L-17"></a> <span class="n">num</span><span class="o">++;</span> </span><span id="L-18"><a name="L-18"></a> <span class="o">}</span> </span><span id="L-19"><a name="L-19"></a> <span class="o">}</span> </span><span id="L-20"><a name="L-20"></a> <span class="k">return</span> <span class="n">imgs</span><span class="o">;</span> </span></pre></div> </td></tr></table>
"buffered image", image, split