这是我的代码,当您选择要裁剪的区域然后按回车键时,裁剪的图像与原始选定区域的大小/图片不同。
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 && 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 && 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 < 0) {
width = 0 - width;
x = x - width + 1;
if (x < 0) {
width += x;
x = 0;
}
}
if (height < 0) {
height = 0 - height;
y = y - height + 1;
if (y < 0) {
height += y;
y = 0;
}
}
if ((x + width) > compWidth) {
width = compWidth - x;
}
if ((y + height) > 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") + "&" + 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("<original>") + 10, readLine.indexOf("</original>")));
}
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();
}
}
}
有人可以查看我的代码并找出为什么它不裁剪完全相同的大小吗?