java - 图像显示使用 p :graphicImage. 图片看起来像负片

标签 java jsf primefaces

我正在使用 Primefaces p:graphicImage 来显示每个登录用户的图像。除了我的照片看起来像底片外,一切都很好。我做错了什么。这是我的代码:

<sec:authorize access="isAuthenticated()">
   <span>
     <p:graphicImage value="#{currentUser.image}" class="img-thumbnail pull-right"
        height="80px;" width="80px;" >
       <f:param name="id" value="#{request.remoteUser}" />
     </p:graphicImage>
   </span>

.....

And here is the JSF Managed Bean


@Named(value = "currentUser")
@ApplicationScoped
public class CurrentUser implements Serializable {

    @EJB
    private VempDetailsFacade vempDetailsFacade;
    private VempDetails details;

    public StreamedContent getImage() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        } else {
            // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
            String imageId = context.getExternalContext().getRequestParameterMap().get("id");
            details = vempDetailsFacade.find(imageId);
            if (details != null) {
                try {
                    return new DefaultStreamedContent(new ByteArrayInputStream(details.getEmpImage()));
                } catch (Exception e) {
                    System.err.println("No Image Retrieved : "+e.getMessage());
                }
            }
            return null;

        }
    }

    public VempDetails getDetails() {
        return details;
    }

    public void setDetails(VempDetails details) {
        this.details = details;
    }

    @PostConstruct
    public void init() {
        details = vempDetailsFacade.
                find(FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
    }

}

我可能做错了什么。同样的图片在一个普通的swing应用程序中显示没有任何问题

代码更新

@BalusC 下面是在 swing 上显示图像的快速测试代码。

public DisplayImage() {
        super("Image Display");
        setSize(600, 600);
        connection = getConnection();
        try { 
            statement = connection
                    .prepareStatement("SELECT empImage FROM v_empDetails WHERE empCode=?");
            statement.setString(1, "009");
            result = statement.executeQuery();

            byte[] image = null;
            //just a result anyway
            while (result.next()) {
                image = result.getBytes(1);         
            }
            Image img = Toolkit.getDefaultToolkit().createImage(image);
            ImageIcon icon = new ImageIcon(img);
            JLabel lPhoto = new JLabel();
            lPhoto.setIcon(icon);
            add(lPhoto);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        setVisible(true);
    }

    public Connection getConnection() {
        Connection connection = null;
........

难道是Toolkit.getDefaultToolkit().createImage(image);行正在重新反转。这些图像是使用不同的应用程序填充的,这更像是一种更新,将其作为基于 Web 的应用程序。

提前感谢您的建议。

最佳答案

JSF/PrimeFaces 只是呈现器,根本不会反转图像。否则世界上任何其他使用 JSF/PrimeFaces 的人都会面临同样的问题。你的问题更深层次。更有可能的是,图像已经作为底片存储在数据库中,并且您现有的 Swing/Java2D 代码的编写方式使其在显示之前重新反转图像,可能是在编写错误的调整大小/裁剪过程中。

因此,要解决此问题,请重新关注负责将这些图像存储在数据库中的代码(并且不要忘记修复 Swing/Java2D 代码以不再重新反转这些代码)。

关于java - 图像显示使用 p :graphicImage. 图片看起来像负片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30018237/

相关文章:

java - 我如何遍历这个数组列表

java - spring mvc javax.validation.UnexpectedTypeException : No validator could be found for type: java. lang.String

jsf - p :selectOneMenu does not work

JSF 组合框 : display items from Collection

java - 使用 JSF 和 PrimeFaces 进行自适应轮询

jsf - 启用选择时,DataModel 必须实现 org.primefaces.model.SelectableDataModel,但我已经定义了 rowKey

ajax - 如何找出ajax更新/渲染组件的客户端ID?找不到从 "foo"引用的表达式为 "bar"的组件

java - 缺少工件 Leadtools :ocr:jar:1. 0

java - 如果我使用 org.apache.hadoop.mapreduce(新)API,如何配置 Hadoop MapReduce 映射器输出压缩?

java - p :ajax using an global HTML id to reference to an <h:messages> attribute