我想从图库中调整图片大小然后上传它。由于用户可能选择可能需要很长时间才能上传的大尺寸图像。假设我想将图像的大小调整为1024px宽度,并根据宽度重新缩放高度。我使用openGallery方法,我找不到任何参数来指定要调整大小的img的宽度。我想如果我没弄错的话,(对于相机img)Capture.capturePhoto(Display.getInstance()。getDisplayWidth()/ 2,-1)可以解决问题。如何从画廊中获取图像?三江源
Display.getInstance().openGallery(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
if (evt == null) {
System.out.println("user cancelled");
return;
}
eventImgpath = (String) evt.getSource();
Image i = Image.createImage(eventImgpath);
eventImage.setIcon(i.scaledWidth(Display.getInstance().getDisplayWidth()));
eventImage.getParent().revalidate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}, Display.GALLERY_IMAGE);
//sending the img path to connectionRequest
String eventImgPathFinal = eventImgpath;
AddBusinessForumConnection abfc = new AddBusinessForumConnection();
abfc.addBusinessForumConnectionMethod(eventImgPathFinal);
上传img
public void addBusinessForumConnectionMethod(String eventImg) {
ConnectionRequest connectionRequest = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
}
@Override
protected void postResponse() {
}
};
AllUrl allUrl = new AllUrl();
connectionRequest.setUrl(allUrl.addBusinessForumUrl);
connectionRequest.setPost(true);
connectionRequest.addArgument("imgUrl", new ImageBase64().getBase64(eventImg));
NetworkManager.getInstance().addToQueueAndWait(connectionRequest);
}
答案 0 :(得分:1)
使用ImageIO调整大小:
ByteArrayOutputStream out = = new ByteArrayOutputStream();
ImageIO.getImageIO().save(path, out, ImageIO.FORMAT_JPEG,
maxWidth, -1, 1);
byte [] data = out.toByteArray();