Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving image as base64 encode #399

Closed
swap17 opened this issue Oct 1, 2015 · 4 comments
Closed

Saving image as base64 encode #399

swap17 opened this issue Oct 1, 2015 · 4 comments
Labels

Comments

@swap17
Copy link

swap17 commented Oct 1, 2015

I am using the below mentioned code in applet for saving image in base64 encode string .On creating image from base64 encoded string the image is cropped its not showing the complete image.please suggest.

ByteArrayOutputStream os = new ByteArrayOutputStream();
final BufferedImage bufferedImage = webcam.getImage();
OutputStream b64 = new Base64.OutputStream(os);
ImageIO.write(bufferedImage, "png", b64);
String imageStr = os.toString("UTF-8");
@sarxos
Copy link
Owner

sarxos commented Oct 1, 2015

Hi @swap17,

Can you please elaborate more on how the image is cropped? Is the vertical or horizontal size smaller?

Moreover, can you check what is the image size you are getting from Webcam:

BufferedImage bufferedImage = webcam.getImage();
System.out.println("width " + bufferedImage.getWidth());
System.out.println("height " + bufferedImage.getHeight());

I also suggest to use JPG instead of PNG (the fist one has a lot smaller size after DCT). Check this out:

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.github.sarxos.webcam.Webcam;


public class Test {

    public static void main(String[] args) throws IOException {

        Webcam webcam = Webcam.getDefault();
        webcam.setCustomViewSizes(new Dimension[] { new Dimension(1280, 720) });
        webcam.setViewSize(new Dimension(1280, 720));
        webcam.open();

        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
        ByteArrayOutputStream baos2 = new ByteArrayOutputStream();

        BufferedImage bi = webcam.getImage();

        ImageIO.write(bi, "JPG", baos1);
        ImageIO.write(bi, "PNG", baos2);

        System.out.println("JPG = " + baos1.size());
        System.out.println("PNG = " + baos2.size());
        System.out.println("ratio = " + (baos2.size() / baos1.size()));
    }
}

The output for HD 720p is:

JPG = 56035
PNG = 1267544
ratio = 22

The JPG compression is 22 times more efficient than PNG (with default compression settings).

@sarxos sarxos added the question label Oct 2, 2015
@sarxos sarxos changed the title saving image as base64 encode Saving image as base64 encode Oct 3, 2015
@sarxos
Copy link
Owner

sarxos commented Oct 3, 2015

I'm closing this ticket. Please comment here if you think it should be reopen.

@sarxos sarxos closed this as completed Oct 3, 2015
@swap17
Copy link
Author

swap17 commented Oct 3, 2015

Thanks sarox.I had used JPG and it solved the issue

@sarxos
Copy link
Owner

sarxos commented Oct 3, 2015

Great :) Take care!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants