resizescalr

Resizing an image from a database using Scalr


I am trying to resize an image from by db. Below is the webservlet where I get the blob from my database and try to resize it using UtilImage.

Problem: The image i tried to resize isn't shown in my browser and my console hasn't listed any stacktraces. After the UtilImage.resize() call, which returns a new BufferedImage, the imageType has changed from 5 to 1.

@WebServlet("/images")
public class BildService extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    private LesenService lesenService;

    private Veranstaltung veranstaltung;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer veranstaltungId = Integer.parseInt(request.getParameter("id"));
        veranstaltung = lesenService.getVeranstaltungByVeranstaltungId(veranstaltungId);

        if (veranstaltung == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }       

        response.setContentType(veranstaltung.getFormat());
        OutputStream os = response.getOutputStream();

        byte[] img = UtilImage.resizeScalr(veranstaltung.getBild(), veranstaltung.getFormat());
        os.write(img);          
    }   
}

This is my UtilImage class:

    public class UtilImage {
    public static final byte[] resizeScalr(byte[] img, String type) {
        if (img != null) {
            BufferedImage bImage = getBufferedImage(img);
            double scaleFactor = getScaleFactor(bImage.getWidth(), UtilKonstanten.PICTURE_WIDTH);
            int targetWidth = (int) (scaleFactor * bImage.getWidth());
            int targetHeight = (int) (scaleFactor * bImage.getHeight());
            // return getByteArray(Scalr.resize(bImage, targetWidth,
            // targetHeight), type);
            return getByteArray(Scalr.resize(bImage, Scalr.Method.BALANCED, targetWidth, targetHeight), type);
        }
        return null;
    }

    private static final double getScaleFactor(int width, int wishwidth) {
        return (double) wishwidth / width;
    }

    private static final BufferedImage getBufferedImage(byte[] array) {
        InputStream is = new ByteArrayInputStream(array);
        BufferedImage img;
        try {
            img = ImageIO.read(is);
            return img;
        } catch (IOException e) {
            // TODO
            e.printStackTrace();
        }
        return null;
    }

    private static final byte[] getByteArray(BufferedImage img, String type) {

        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(img, type, os);
            byte[] byteArray = os.toByteArray();
            os.close();
            return byteArray;
        } catch (IOException e) {
            // TODO
            e.printStackTrace();
        }
        return null;
    }
}

Thanks a lot for your help!


Solution

  • Unfortunately I didn't post the answer to this question. As far as I know it was something with the type, that wasn't saved correctly and in cause of that the browser wasn't able to resolve the type. --> "img" instead of "application/img" or something like that.