【问题标题】:Resize the image before uploading images from openGallery method在从 openGallery 方法上传图像之前调整图像大小
【发布时间】:2016-08-07 00:37:11
【问题描述】:

我想调整图库中的图片大小,然后上传。由于用户可能会选择可能需要很长时间才能上传的大尺寸图像。假设我想将图像的大小调整为 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);

上传图片

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);
}

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    使用 ImageIO 调整大小:

        ByteArrayOutputStream out = = new ByteArrayOutputStream();
    
        ImageIO.getImageIO().save(path, out, ImageIO.FORMAT_JPEG,
                maxWidth, -1, 1);
    
        byte [] data = out.toByteArray();
    

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多