Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2009-2013 PrimeTek.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.primefaces.component.imagecropper;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Map;
- import javax.faces.application.Resource;
- import javax.faces.component.UIComponent;
- import javax.faces.component.UIParameter;
- import javax.faces.context.ExternalContext;
- import javax.faces.context.FacesContext;
- import javax.faces.context.ResponseWriter;
- import javax.faces.convert.ConverterException;
- import javax.imageio.ImageIO;
- import org.primefaces.context.RequestContext;
- import org.primefaces.model.CroppedImage;
- import org.primefaces.model.StreamedContent;
- import org.primefaces.renderkit.CoreRenderer;
- import org.primefaces.util.Constants;
- import org.primefaces.util.StringEncrypter;
- import org.primefaces.util.WidgetBuilder;
- public class ImageCropperRenderer extends CoreRenderer {
- @Override
- public void decode(FacesContext context, UIComponent component) {
- ImageCropper cropper = (ImageCropper) component;
- Map<String, String> params = context.getExternalContext().getRequestParameterMap();
- String coordsParam = cropper.getClientId(context) + "_coords";
- if (params.containsKey(coordsParam)) {
- cropper.setSubmittedValue(params.get(coordsParam));
- }
- }
- @Override
- public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
- ImageCropper cropper = (ImageCropper) component;
- encodeMarkup(context, cropper);
- encodeScript(context, cropper);
- }
- protected void encodeScript(FacesContext context, ImageCropper cropper) throws IOException {
- String widgetVar = cropper.resolveWidgetVar();
- String clientId = cropper.getClientId(context);
- String image = clientId + "_image";
- String select = null;
- WidgetBuilder wb = getWidgetBuilder(context);
- wb.initWithComponentLoad("ImageCropper", widgetVar, clientId, clientId + "_image", "imagecropper")
- .attr("image", image);
- if (cropper.getMinSize() != null) {
- wb.append(",minSize:[").append(cropper.getMinSize()).append("]");
- }
- if (cropper.getMaxSize() != null) {
- wb.append(",maxSize:[").append(cropper.getMaxSize()).append("]");
- }
- wb.attr("bgColor", cropper.getBackgroundColor(), null)
- .attr("bgOpacity", cropper.getBackgroundOpacity(), 0.6)
- .attr("aspectRatio", cropper.getAspectRatio(), Double.MIN_VALUE);
- if (cropper.getValue() != null) {
- CroppedImage croppedImage = (CroppedImage) cropper.getValue();
- int x = croppedImage.getLeft();
- int y = croppedImage.getTop();
- int x2 = x + croppedImage.getWidth();
- int y2 = y + croppedImage.getHeight();
- select = "[" + x + "," + y + "," + x2 + "," + y2 + "]";
- } else if (cropper.getInitialCoords() != null) {
- select = "[" + cropper.getInitialCoords() + "]";
- }
- wb.append(",setSelect:").append(select);
- wb.finish();
- }
- protected void encodeMarkup(FacesContext context, ImageCropper cropper) throws IOException {
- ResponseWriter writer = context.getResponseWriter();
- String clientId = cropper.getClientId(context);
- String coordsHolderId = clientId + "_coords";
- writer.startElement("div", cropper);
- writer.writeAttribute("id", clientId, null);
- renderImage(context, cropper, clientId);
- writer.startElement("input", null);
- writer.writeAttribute("type", "hidden", null);
- writer.writeAttribute("id", coordsHolderId, null);
- writer.writeAttribute("name", coordsHolderId, null);
- writer.endElement("input");
- writer.endElement("div");
- }
- @Override
- public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException {
- String coords = (String) submittedValue;
- if (isValueBlank(coords)) {
- return null;
- }
- ImageCropper cropper = (ImageCropper) component;
- //remove query string
- Object imagePath = cropper.getImage();
- if (imagePath instanceof String) {
- int queryStringIndex = ((String) imagePath).indexOf("?");
- if (queryStringIndex != -1) {
- imagePath = ((String) imagePath).substring(0, queryStringIndex);
- }
- }
- String[] cropCoords = coords.split("_");
- String format = getFormat(imagePath);
- int x = Integer.parseInt(cropCoords[0]);
- int y = Integer.parseInt(cropCoords[1]);
- int w = Integer.parseInt(cropCoords[2]);
- int h = Integer.parseInt(cropCoords[3]);
- try {
- BufferedImage outputImage = getSourceImage(context, imagePath);
- BufferedImage cropped = outputImage.getSubimage(x, y, w, h);
- ByteArrayOutputStream croppedOutImage = new ByteArrayOutputStream();
- ImageIO.write(cropped, format, croppedOutImage);
- return new CroppedImage(new String(), croppedOutImage.toByteArray(), x, y, w, h); // FIX THIS
- } catch (IOException e) {
- throw new ConverterException(e);
- }
- }
- private void renderImage(FacesContext context, ImageCropper cropper, String clientId) throws IOException {
- ResponseWriter writer = context.getResponseWriter();
- String alt = cropper.getAlt() == null ? "" : cropper.getAlt();
- String imageSrc;
- try {
- imageSrc = getImageSrc(context, cropper);
- } catch (Exception e) {
- throw new IOException(e);
- }
- writer.startElement("img", null);
- writer.writeAttribute("id", clientId + "_image", null);
- writer.writeAttribute("alt", alt, null);
- writer.writeAttribute("src", imageSrc, null);
- writer.endElement("img");
- }
- protected String getFormat(Object path) {
- if (path instanceof String) {
- String[] pathTokens = ((String) path).split("\\.");
- return pathTokens[pathTokens.length - 1];
- } else if (path instanceof StreamedContent) {
- StreamedContent content = (StreamedContent) path;
- return mapMimeTypeToFormat(content.getContentType());
- }
- return "";
- }
- protected boolean isExternalImage(ImageCropper cropper) {
- return (cropper.getImage() instanceof String) && ((String) cropper.getImage()).startsWith("http");
- }
- private BufferedImage getSourceImage(FacesContext context, Object imagePath) throws IOException {
- BufferedImage outputImage = null;
- if (imagePath instanceof String) {
- boolean isExternal = ((String) imagePath).startsWith("http");
- if (isExternal) {
- URL url = new URL((String) imagePath);
- outputImage = ImageIO.read(url);
- } else {
- ExternalContext externalContext = context.getExternalContext();
- outputImage = ImageIO.read(new File(externalContext.getRealPath("") + (String) imagePath));
- }
- } else if (imagePath instanceof StreamedContent) {
- outputImage = ImageIO.read(((StreamedContent) imagePath).getStream());
- }
- return outputImage;
- }
- /**
- * Additional method to handle StreamedContent image attribute
- *
- * @param context The current FacesContext
- * @param cropper The imageCropper Object
- * @return
- */
- private String getImageSrc(FacesContext context, ImageCropper cropper) throws Exception {
- Object image = cropper.getImage();
- if (image instanceof String) {
- return getResourceURL(context, (String) cropper.getImage());
- } else if (image instanceof StreamedContent) {
- // Copied from GraphicImageRenderer
- StreamedContent streamedContent = (StreamedContent) image;
- Resource resource = context.getApplication().getResourceHandler().createResource("dynamiccontent.properties", "primefaces", streamedContent.getContentType());
- String resourcePath = resource.getRequestPath();
- StringEncrypter strEn = RequestContext.getCurrentInstance().getEncrypter();
- String rid = strEn.encrypt(cropper.getValueExpression("image").getExpressionString());
- StringBuilder builder = new StringBuilder(resourcePath);
- builder.append("&").append(Constants.DYNAMIC_CONTENT_PARAM).append("=").append(URLEncoder.encode(rid, "UTF-8"));
- for (UIComponent kid : cropper.getChildren()) {
- if (kid instanceof UIParameter) {
- UIParameter param = (UIParameter) kid;
- Object paramValue = param.getValue();
- builder.append("&").append(param.getName()).append("=");
- if (paramValue != null) {
- builder.append(URLEncoder.encode(param.getValue().toString(), "UTF-8"));
- }
- }
- }
- return builder.toString();
- }
- return "";
- }
- /**
- * Helper method to map mimeTypes from StreamedContent to proper ImageIO
- * format string. Updated for JDK7.
- *
- * @param mimeType The mime type to be mapped.
- * @return The proper ImageIO format string.
- */
- private String mapMimeTypeToFormat(String mimeType) {
- if (mimeType != null) {
- if (mimeType.equalsIgnoreCase("image/jpeg")) {
- return "jpg";
- } else if (mimeType.equalsIgnoreCase("image/gif")) {
- return "gif";
- } else if (mimeType.equalsIgnoreCase("image/bmp")) {
- return "bmp";
- } else if (mimeType.equalsIgnoreCase("image/vnd.wap.wbmp")) {
- return "wbmp";
- } else if (mimeType.equalsIgnoreCase("image/png")) {
- return "png";
- }
- }
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement