title: Crop image to square - Android
body: <p>How can I cut rectangular image (600 x 300) from left and right to fit in square ImageView ? I don't want to resize image, I just want to crop it, to be 300 x 300.</p>
  
  <p><strong>[SOLUTION]</strong></p>
  
  <p>As @blackbelt said </p>
  
  <p><code>Bitmap cropImg = Bitmap.createBitmap(src, startX, startY, dstWidth, dstHeight);</code></p>
  
  <p>is great for cropping images. So how can you automatically crop images with different sizes. I create this simple code for that:</p>
  
  <pre><code>// From drawable
  Bitmap src= BitmapFactory.decodeResource(context.getResources(), R.drawable.image);
  
  // From URL
  Bitmap src = null;
  try {
      String URL = "http://www.example.com/image.jpg";
      InputStream in = new java.net.URL(URL).openStream();
      src = BitmapFactory.decodeStream(in);
  } catch (Exception e) {
      e.printStackTrace();
  }
  
  int width = src.getWidth();
  int height = src.getHeight();
  int crop = (width - height) / 2;
  Bitmap cropImg = Bitmap.createBitmap(src, crop, 0, height, height);
  
  ImageView.setImageBitmap(cropImg);
  </code></pre>