Help, p:imageSwitch

UI Components for JSF
Post Reply
armdev
Posts: 52
Joined: 26 May 2009, 16:25

03 Nov 2010, 11:19

hello All, I have too bad problem.
I have image list from database(id -s) and servlet who show images

Code: Select all

 <p:imageSwitch effect="SingleDoor" speed="800" slideshowAuto="false" widgetVar="img">
                                <c:forEach items="#{GalleryBean.galleryList}" var="list">
                                    <p:graphicImage cache="false" value="/ImageShow?fileId=#{list.imageId}"/>
                                </c:forEach>
            </p:imageSwitch>
IN my localhost images switch very good, but when I put war to the server, images does not show.
I inspect elements and found what in the server show content type not image/jpeg (text/html), do not show image
I dow not know problem connected with graphicImage or imageSwitch ?
herse isa link to site http://inalep.com/wheretogo.jsf
Why in my localhost is working in server not?
I tryed a lot of versions:)) Help


here is a servlet

Code: Select all

private void doPreviewImage(HttpServletRequest request, HttpServletResponse response) {
    String fileIdStr = request.getParameter("fileId");    
    //String widthStr = request.getParameter("w");
    String widthStr = "300";

    Long fileId = null;
    if (fileIdStr != null) {
      try {
        fileId = Long.parseLong(fileIdStr);
      } catch (NumberFormatException e) {
      }
    }
    int width = 0;
    if (widthStr != null && widthStr.length() > 0) {
      try {
        width = Integer.parseInt(widthStr);
      } catch (NumberFormatException e) {
      }
    }
    if (fileId != null) {
      FileDTO file = null;
      FileManager fileAction = FileActionFactory.getFilesInstance();
      if(fileAction != null){
        file = fileAction.getFile(fileId);
        fileAction.release();
      }
      if (file != null) {
        byte[] content = file.getContent();
        if (content != null) {
          String mimeType = file.getMimetype();
          System.out.println("mimeType " + mimeType);

          response.addHeader("Pragma", "cache");
          response.addHeader("Cache-Control", "max-age=3600, must-revalidate");
          response.addDateHeader("Expires", System.currentTimeMillis() + 1000*3600*10);
          response.setContentType("image/jpeg");
          try {
            if(mimeType != null){
              ByteArrayInputStream bi = new ByteArrayInputStream(content);
              InputStream thumbStream = scaleImageJPG(bi,width);
              byte[] thumbContent = new byte[thumbStream.available()];
              thumbStream.read(thumbContent);
              response.getOutputStream().write(thumbContent);               
            }
          } catch (IOException e) {
            System.out.println("file content send error");
          } catch (Exception e) {
            System.out.println("file exception: " + e);
          } finally {
            content = null;
            file = null;
          }
          return;
        }
      }
      else{
        //TODO add page not found
        response.addHeader("Pragma", "no-cache");
        response.addDateHeader("Expires", System.currentTimeMillis() - 1000*3600);
        try{
          response.getWriter().println("file object is null");
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return;
      }
    }
    //TODO add page not found
    response.addHeader("Pragma", "no-cache");
    response.addDateHeader("Expires", System.currentTimeMillis() - 1000*3600);

    try{
      response.getWriter().println("file id is not set");
    }
    catch(Exception e){
    }
   // log.debug("file ID parameter is not set or file is not found");
    return;
  }
  // </editor-fold>

  public static InputStream scaleImageJPG(InputStream p_image, int p_width) throws Exception {

    InputStream imageStream = new BufferedInputStream(p_image);
    Image image = (Image) ImageIO.read(imageStream);

    int thumbWidth = p_width;
    int thumbHeight = 0;

    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;
    thumbHeight = (int) (thumbWidth / imageRatio);

    // Draw the scaled image
    BufferedImage thumbImage = new BufferedImage(thumbWidth,
            thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    // Write the scaled image to the outputstream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
    int quality = 100; // Use between 1 and 100, with 100 being highest quality

    quality = Math.max(0, Math.min(quality, 100));
    param.setQuality((float) quality / 100.0f, false);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(thumbImage);
    ImageIO.write(thumbImage, "IMAGE_JPG", out);

    // Read the outputstream into the inputstream for the return value
    ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());

    return bis;
  }

armdev
Posts: 52
Joined: 26 May 2009, 16:25

03 Nov 2010, 17:28

I found may be problem connected with libs what I use for encoding jpegs in servlet and they no found in the server side.

Code: Select all

//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGEncodeParam;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;

Code: Select all

 public static InputStream scaleImageJPG(InputStream p_image, int p_width) throws Exception {
      
    InputStream imageStream = new BufferedInputStream(p_image);
    Image image = (Image) ImageIO.read(imageStream);

    int thumbWidth = p_width;
    int thumbHeight = 0;

    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;
    thumbHeight = (int) (thumbWidth / imageRatio);

    // Draw the scaled image
    BufferedImage thumbImage = new BufferedImage(thumbWidth,
            thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    // Write the scaled image to the outputstream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
    int quality = 100; // Use between 1 and 100, with 100 being highest quality

    quality = Math.max(0, Math.min(quality, 100));
    param.setQuality((float) quality / 100.0f, false);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(thumbImage);
    ImageIO.write(thumbImage, "IMAGE_JPG", out);

    // Read the outputstream into the inputstream for the return value
    ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());

    return bis;
  }
what I can use alternatively instead
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

armdev
Posts: 52
Joined: 26 May 2009, 16:25

03 Nov 2010, 17:29

Also width="50" height="50" or width="50px" height="50px" have not affect on p:graphicImage, why?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 15 guests