Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Image Cropping Not Accurate
    primarykey
    data
    text
    <p>Heres my code, when you select and area you want to crop and then push enter, the cropped image isn't the same size/picture as the original selected area.</p> <pre><code>public class DragNDrop extends JFrame implements DropTargetListener { private static final long serialVersionUID = 1872019741456690593L; private Graphics g; private BufferedImage image, origiImage; private Rectangle area; private Rectangle currentRect; private Rectangle rectToDraw = null; private Image buffer; public static void main(String args[]) { new DragNDrop(); } public DragNDrop() { super("Drop Test"); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDropTarget(new DropTarget(getContentPane(), this)); setVisible(true); CaptureListener listener = new CaptureListener(); addMouseListener(listener); addMouseMotionListener(listener); } public void drop(DropTargetDropEvent dtde) { try { Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); Object list = tr.getTransferData(flavors[0]); list = list.toString().substring(1, list.toString().length()-1); if (isValidImage(list)) { Image droppedImage = Toolkit.getDefaultToolkit().getImage(list.toString()); image = toBufferedImage(droppedImage); origiImage = toBufferedImage(droppedImage); area = new Rectangle(image.getWidth(), image.getHeight()); if (droppedImage != null) { setSize(image.getWidth(), image.getHeight()); dtde.dropComplete(true); addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == 10) { capture(); } } @Override public void keyPressed(KeyEvent e) { } }); return; } } dtde.rejectDrop(); } catch (Exception e) { dtde.rejectDrop(); } } public void paint() { if (area != null &amp;&amp; image != null) { g.clearRect(area.x, area.y, area.width, area.height); g.drawImage(image, 0, 0, null); } if (currentRect != null) { g.setColor(Color.RED); g.drawRect(rectToDraw.x, rectToDraw.y, rectToDraw.width, rectToDraw.height); g.setColor(new Color(255,255,255,150)); g.fillRect(rectToDraw.x, rectToDraw.y, rectToDraw.width, rectToDraw.height); } } @Override public void paint(Graphics gr) { if (buffer == null &amp;&amp; area != null) { this.buffer = createImage(area.width, area.height); this.g = buffer.getGraphics(); } paint(); if (buffer != null) gr.drawImage(buffer, 0, 0, this); } public boolean isValidImage(Object list) { System.out.println(list.toString()); for (String string : ImageIO.getReaderFormatNames()) if (list.toString().contains(string)) return true; return false; } public BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } image = new ImageIcon(image).getImage(); boolean hasAlpha = hasAlpha(image); BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); int transparency = Transparency.OPAQUE; if (hasAlpha == true) { transparency = Transparency.BITMASK; } GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); if (bimage == null) { int type = BufferedImage.TYPE_INT_RGB; if (hasAlpha == true) { type = BufferedImage.TYPE_INT_ARGB; } bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage; } public static boolean hasAlpha(Image image) { if (image instanceof BufferedImage) { return ((BufferedImage) image).getColorModel().hasAlpha(); } PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); try { pg.grabPixels(); } catch (InterruptedException e) { } return pg.getColorModel().hasAlpha(); } private void updateRectangle(int compWidth, int compHeight) { int x = currentRect.x; int y = currentRect.y; int width = currentRect.width; int height = currentRect.height; if (width &lt; 0) { width = 0 - width; x = x - width + 1; if (x &lt; 0) { width += x; x = 0; } } if (height &lt; 0) { height = 0 - height; y = y - height + 1; if (y &lt; 0) { height += y; y = 0; } } if ((x + width) &gt; compWidth) { width = compWidth - x; } if ((y + height) &gt; compHeight) { height = compHeight - y; } if (rectToDraw != null) { rectToDraw.setBounds(x, y, width, height); } else { rectToDraw = new Rectangle(x, y, width, height); } } public void capture() { BufferedImage croppedImage = origiImage.getSubimage(rectToDraw.x, rectToDraw.y, rectToDraw.width, rectToDraw.height); setSize(rectToDraw.width, rectToDraw.height); image = croppedImage; } public void upload(BufferedImage image) { String IMGUR_POST_URI = "http://api.imgur.com/2/upload.xml"; String IMGUR_API_KEY = "b84e430b4a65d16a6955358141f21a61"; String readLine = null; try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "png", outputStream); URL url = new URL(IMGUR_POST_URI); String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(outputStream.toByteArray()).toString(), "UTF-8") + "&amp;" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(IMGUR_API_KEY, "UTF-8"); URLConnection urlConnection = url.openConnection(); urlConnection.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream()); wr.write(data); wr.flush(); // Get the response InputStream inputStream; if (((HttpURLConnection) urlConnection).getResponseCode() == 400) { inputStream = ((HttpURLConnection) urlConnection).getErrorStream(); } else { inputStream = urlConnection.getInputStream(); } BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { readLine = line; } wr.close(); reader.close(); } catch(Exception e){ e.printStackTrace(); } //Prints the url link of the image uploaded System.out.println(readLine.substring(readLine.indexOf("&lt;original&gt;") + 10, readLine.indexOf("&lt;/original&gt;"))); } public void dragEnter(DropTargetDragEvent dtde) { } public void dragExit(DropTargetEvent dte) { } public void dragOver(DropTargetDragEvent dtde) { } public void dropActionChanged(DropTargetDragEvent dtde) { } private class CaptureListener extends MouseInputAdapter { public void mouseDragged(MouseEvent e) { updateSize(e); } public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); currentRect = new Rectangle(x, y, 0, 0); updateRectangle(getWidth(), getHeight()); repaint(); } public void mouseReleased(MouseEvent e) { updateSize(e); } public void updateSize(MouseEvent e) { currentRect.setSize(e.getX() - currentRect.x, e.getY() - currentRect.y); updateRectangle(getWidth(), getHeight()); repaint(); } } } </code></pre> <p>Can someone look through my code and find why it doesn't crop the exact same size?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload