how to resize java bufferedimage resolution

时间:2018-02-03 11:06:21

标签: java sockets bufferedimage

Resize Buffered Image

im try to make remote desktop application so i need to send low quality images to the client. anyone tell me how to resize buffered image resolution

 try {
        while (true) {
            Socket socket = new Socket(connect.ConnectionDetails.clientip, connect.ConnectionDetails.RemoteDesktopFeedSendPort);
            BufferedImage screenshot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(screenshot, "png", os);
            InputStream fis = new ByteArrayInputStream(os.toByteArray());
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(buffer);
            socket.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

This is a class I usually consider to do resizing on png.

I hope fits your needs:

public class pngResizer {

    public static BufferedImage convertRGBAToIndexed(BufferedImage src) {
        BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
        Graphics g = dest.getGraphics();
        g.setColor(new Color(231, 20, 189));
        g.fillRect(0, 0, dest.getWidth(), dest.getHeight());
        dest = makeTransparent(dest, 0, 0);
        dest.createGraphics().drawImage(src, 0, 0, null);
        return dest;
    }

    public static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
        ColorModel cm = image.getColorModel();
        if (!(cm instanceof IndexColorModel))
            return image; // sorry...
        IndexColorModel icm = (IndexColorModel) cm;
        WritableRaster raster = image.getRaster();
        int pixel = raster.getSample(x, y, 0);
        int size = icm.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        icm.getReds(reds);
        icm.getGreens(greens);
        icm.getBlues(blues);
        IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
        return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
    }

    public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
        int original_width = imgSize.width;
        int original_height = imgSize.height;
        int bound_width = boundary.width;
        int bound_height = boundary.height;
        int new_width = 0;
        int new_height = 0;

        if (original_width > original_height) {
            new_width = bound_width;
            new_height = (new_width*original_height)/original_width;
        } else {
            new_height = bound_height;
            new_width = (new_height*original_width)/original_height;
        }

        return new Dimension(new_width, new_height);
    }

    public static void resizeImage(File original_image, File resized_image, int IMG_SIZE) throws IOException {

            BufferedImage originalImage = ImageIO.read(original_image);

            String extension = "png";

            int type = extension.equals("gif") || (originalImage.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

            Dimension new_dim = getScaledDimension(new Dimension(originalImage.getWidth(), originalImage.getHeight()), new Dimension(IMG_SIZE,IMG_SIZE));

            BufferedImage resizedImage = new BufferedImage((int) new_dim.getWidth(), (int) new_dim.getHeight(), type);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(originalImage, 0, 0, (int) new_dim.getWidth(), (int) new_dim.getHeight(), null);
            g.dispose();            

            if (!extension.equals("gif")) {
                ImageIO.write(resizedImage, extension, resized_image);
            } else {
                // Gif Transparence workarround
                ImageIO.write(convertRGBAToIndexed(resizedImage), "gif", resized_image);
            }

    }

    public static void main(String[] args) throws IOException {
        int xReductionFactor = 96;//  224
        File input = new File("C:/originalSize2");
          File output = new File("C:/convert"); 
          for(File f : input.listFiles()){
              if(!f.getName().endsWith("png")){
                  continue;
              }

              resizeImage(f,new File(output+"\\"+f.getName()),xReductionFactor);
          }
          System.out.println(  xReductionFactor + "px finished.");
    }  
}