Java Graphics Rotate rotate(byte[] bytes)

Here you can find the source of rotate(byte[] bytes)

Description

rotate

License

Open Source License

Declaration

public static byte[] rotate(byte[] bytes) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import java.io.*;

public class Main {

    public static byte[] rotate(byte[] bytes) throws IOException {

        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        BufferedImage src = ImageIO.read(inputStream);

        int height = src.getHeight();
        AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(90), height / 2, height / 2);
        AffineTransformOp transformOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);

        BufferedImage dst = new BufferedImage(src.getHeight(), src.getWidth(), src.getType());
        transformOp.filter(src, dst);/*ww  w .  j  a  v a2s  . co  m*/

        BufferedOutputStream stream = new BufferedOutputStream(outputStream);
        dst.flush();
        ImageIO.write(dst, "JPEG", outputStream);
        stream.flush();
        stream.close();
        return outputStream.toByteArray();
    }

    public static void write(byte[] bytes, String pathname) throws IOException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        FileOutputStream outputStream = new FileOutputStream(pathname);

        BufferedImage image = ImageIO.read(inputStream);
        ImageIO.write(image, "JPEG", outputStream);

        outputStream.flush();
        outputStream.close();
    }
}

Related

  1. rotate(AffineTransform transform, float angleInDegrees)
  2. rotate(Dimension rect)
  3. rotate(final Point2D pos, final Point2D center, final double dist)
  4. rotate(final Point2D vec, final double theta)
  5. rotate(Point p, double angle)