Java Code Examples for org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_JPEG

The following examples show how to use org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_JPEG . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HSSFPictureData.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return the POI internal image type, -1 if not unknown image type
 *
 * @see Workbook#PICTURE_TYPE_DIB
 * @see Workbook#PICTURE_TYPE_EMF
 * @see Workbook#PICTURE_TYPE_JPEG
 * @see Workbook#PICTURE_TYPE_PICT
 * @see Workbook#PICTURE_TYPE_PNG
 * @see Workbook#PICTURE_TYPE_WMF
 */
public int getPictureType() {
    switch (blip.getRecordId()) {
        case EscherMetafileBlip.RECORD_ID_WMF:
            return Workbook.PICTURE_TYPE_WMF;
        case EscherMetafileBlip.RECORD_ID_EMF:
            return Workbook.PICTURE_TYPE_EMF;
        case EscherMetafileBlip.RECORD_ID_PICT:
            return Workbook.PICTURE_TYPE_PICT;
        case EscherBitmapBlip.RECORD_ID_PNG:
            return Workbook.PICTURE_TYPE_PNG;
        case EscherBitmapBlip.RECORD_ID_JPEG:
            return Workbook.PICTURE_TYPE_JPEG;
        case EscherBitmapBlip.RECORD_ID_DIB:
            return Workbook.PICTURE_TYPE_DIB;
        default:
            return -1;
    }        
}
 
Example 2
Source File: ExcelImageHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private int getImageFormat( final ResourceKey key ) {
  final URL url = resourceManager.toURL( key );
  if ( url == null ) {
    return -1;
  }

  final String file = url.getFile();
  if ( StringUtils.endsWithIgnoreCase( file, ".png" ) ) { // NON-NLS
    return Workbook.PICTURE_TYPE_PNG;
  }
  if ( StringUtils.endsWithIgnoreCase( file, ".jpg" ) || // NON-NLS
      StringUtils.endsWithIgnoreCase( file, ".jpeg" ) ) { // NON-NLS
    return Workbook.PICTURE_TYPE_JPEG;
  }
  if ( StringUtils.endsWithIgnoreCase( file, ".bmp" ) || // NON-NLS
      StringUtils.endsWithIgnoreCase( file, ".ico" ) ) { // NON-NLS
    return Workbook.PICTURE_TYPE_DIB;
  }
  return -1;
}
 
Example 3
Source File: ExcelExportBase.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
/**
 * 获取图片类型,设置图片插入类型
 * 
 * @param value
 * @return
 * @Author JueYue
 * @date 2013年11月25日
 */
public int getImageType(byte[] value) {
	String type = PoiPublicUtil.getFileExtendName(value);
	if (type.equalsIgnoreCase("JPG")) {
		return Workbook.PICTURE_TYPE_JPEG;
	} else if (type.equalsIgnoreCase("PNG")) {
		return Workbook.PICTURE_TYPE_PNG;
	}
	return Workbook.PICTURE_TYPE_JPEG;
}
 
Example 4
Source File: Excel97Producer.java    From ureport with Apache License 2.0 5 votes vote down vote up
private int buildImageFormat(Image img){
	int type=Workbook.PICTURE_TYPE_PNG;
	String path=img.getPath();
	if(path==null){
		return type;
	}
	path=path.toLowerCase();
	if(path.endsWith("jpg") || path.endsWith("jpeg")){
		type=Workbook.PICTURE_TYPE_JPEG;
	}
	return type;
}
 
Example 5
Source File: ExcelBuilder.java    From ureport with Apache License 2.0 5 votes vote down vote up
protected int buildImageFormat(Image img){
	int type=Workbook.PICTURE_TYPE_PNG;
	String path=img.getPath();
	if(path==null){
		return type;
	}
	path=path.toLowerCase();
	if(path.endsWith("jpg") || path.endsWith("jpeg")){
		type=Workbook.PICTURE_TYPE_JPEG;
	}
	return type;
}
 
Example 6
Source File: ExcelExportBase.java    From easypoi with Apache License 2.0 5 votes vote down vote up
/**
 * 获取图片类型,设置图片插入类型
 * 
 * @param value
 * @return
 * @Author JueYue
 * @date 2013年11月25日
 */
public int getImageType(byte[] value) {
    String type = PoiPublicUtil.getFileExtendName(value);
    if (type.equalsIgnoreCase("JPG")) {
        return Workbook.PICTURE_TYPE_JPEG;
    } else if (type.equalsIgnoreCase("PNG")) {
        return Workbook.PICTURE_TYPE_PNG;
    }
    return Workbook.PICTURE_TYPE_JPEG;
}
 
Example 7
Source File: ImageInfo.java    From ermasterr with Apache License 2.0 5 votes vote down vote up
public int getExcelPictureType() throws InputException {
    if (format == SWT.IMAGE_JPEG) {
        return Workbook.PICTURE_TYPE_JPEG;

    } else if (format == SWT.IMAGE_PNG) {
        return Workbook.PICTURE_TYPE_PNG;

    } else {
        throw new InputException("dialog.message.export.image.not.supported");
    }
}
 
Example 8
Source File: StyleManagerUtils.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * <p>
 * Convert a MIME string into a Workbook.PICTURE* constant.
 * </p><p>
 * In some cases BIRT fails to submit a MIME string, in which case this method falls back to basic data signatures for JPEG and PNG images.
 * <p>
 * @param mimeType
 * The MIME type.
 * @param data
 * The image data to consider if no recognisable MIME type is provided.
 * @return
 * A Workbook.PICTURE* constant.
 */
public int poiImageTypeFromMimeType( String mimeType, byte[] data ) {
	if( "image/jpeg".equals(mimeType) ) {
		return Workbook.PICTURE_TYPE_JPEG;
	} else if( "image/png".equals(mimeType) ) {
		return Workbook.PICTURE_TYPE_PNG;
	} else if ( "image/bmp".equals( mimeType ) ) {
		return Workbook.PICTURE_TYPE_DIB;
	} else {
		if( null != data ) {
			log.debug( "Data bytes: "
					+ " " + Integer.toHexString( data[0] ).toUpperCase()  
					+ " " + Integer.toHexString( data[1] ).toUpperCase()  
					+ " " + Integer.toHexString( data[2] ).toUpperCase()
					+ " " + Integer.toHexString( data[3] ).toUpperCase()
					);
			if( ( data.length > 2 )
					&& ( data[0] == (byte)0xFF)
					&& ( data[1] == (byte)0xD8) 
					&& ( data[2] == (byte)0xFF)
					) {
				return Workbook.PICTURE_TYPE_JPEG;
			}
			if( ( data.length > 4 )
					&& ( data[0] == (byte)0x89)
					&& ( data[1] == (byte)'P') 
					&& ( data[2] == (byte)'N') 
					&& ( data[3] == (byte)'G') 
					) {
				return Workbook.PICTURE_TYPE_PNG;
			}
		} 
		return 0;
	}
}
 
Example 9
Source File: ImageUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the dimension of this image
 *
 * @param is the stream containing the image data
 * @param type type of the picture: {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_JPEG},
 * {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_PNG} or {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_DIB}
 *
 * @return image dimension in pixels
 */
public static Dimension getImageDimension(InputStream is, int type){
    Dimension size = new Dimension();

    switch (type){
        //we can calculate the preferred size only for JPEG, PNG and BMP
        //other formats like WMF, EMF and PICT are not supported in Java
        case Workbook.PICTURE_TYPE_JPEG:
        case Workbook.PICTURE_TYPE_PNG:
        case Workbook.PICTURE_TYPE_DIB:
            try {
                //read the image using javax.imageio.*
                ImageInputStream iis = ImageIO.createImageInputStream( is );
                try {
                    Iterator<ImageReader> i = ImageIO.getImageReaders( iis );
                    ImageReader r = i.next();
                    try {
                        r.setInput( iis );
                        BufferedImage img = r.read(0);
    
                        int[] dpi = getResolution(r);
    
                        //if DPI is zero then assume standard 96 DPI
                        //since cannot divide by zero
                        if (dpi[0] == 0) dpi[0] = PIXEL_DPI;
                        if (dpi[1] == 0) dpi[1] = PIXEL_DPI;
    
                        size.width = img.getWidth()*PIXEL_DPI/dpi[0];
                        size.height = img.getHeight()*PIXEL_DPI/dpi[1];
                    } finally {
                        r.dispose();
                    }
                } finally {
                    iis.close();
                }

            } catch (IOException e) {
                //silently return if ImageIO failed to read the image
                logger.log(POILogger.WARN, e);
            }

            break;
        default:
            logger.log(POILogger.WARN, "Only JPEG, PNG and DIB pictures can be automatically sized");
    }
    return size;
}
 
Example 10
Source File: AbstractExcelFactory.java    From myexcel with Apache License 2.0 4 votes vote down vote up
private void setImage(Td td, Sheet sheet) {
    if (td.getFile() == null) {
        return;
    }
    try {
        if (createHelper == null) {
            createHelper = workbook.getCreationHelper();
        }
        byte[] bytes = Files.readAllBytes(td.getFile().toPath());
        String fileName = td.getFile().getName();
        int format;
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        switch (suffix) {
            case "jpg":
            case "jpeg":
                format = Workbook.PICTURE_TYPE_JPEG;
                break;
            case "png":
                format = Workbook.PICTURE_TYPE_PNG;
                break;
            case "dib":
                format = Workbook.PICTURE_TYPE_DIB;
                break;
            case "emf":
                format = Workbook.PICTURE_TYPE_EMF;
                break;
            case "pict":
                format = Workbook.PICTURE_TYPE_PICT;
                break;
            case "wmf":
                format = Workbook.PICTURE_TYPE_WMF;
                break;
            default:
                throw new IllegalArgumentException("Invalid image type");
        }
        int pictureIdx = workbook.addPicture(bytes, format);
        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = createHelper.createClientAnchor();
        anchor.setCol1(td.getCol());
        anchor.setRow1(td.getRow());
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        pict.resize(1, 1);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}