1
votes

I want to resize the image from the gallery and then upload it. Since users may select large sized images that may take much time to upload. Say I want to resize the image to 1024px width and the height rescaling with respect to the width. I used openGallery method where I don't find any parameter for specifying the width for the img to be resized. I think if I am not mistaken, (for camera img) Capture.capturePhoto(Display.getInstance().getDisplayWidth() / 2, -1) does the trick. How can I achieve it for the images from the gallery? Thankyou

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

Uploading the 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);
}
1

1 Answers

1
votes

Use the ImageIO to resize:

    ByteArrayOutputStream out = = new ByteArrayOutputStream();

    ImageIO.getImageIO().save(path, out, ImageIO.FORMAT_JPEG,
            maxWidth, -1, 1);

    byte [] data = out.toByteArray();