Java Code Examples for java.awt.image.BufferedImage.getHeight()

The following are Jave code examples for showing how to use getHeight() of the java.awt.image.BufferedImage class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: png_to_jpg_in_svg   File: PNG_to_JPG_in_SVG.java   View Source Code Vote up 7 votes
private String convertPngToJpeg(String pngBase64) throws IOException {
	byte[] pngBinary = DatatypeConverter.parseBase64Binary(pngBase64);
	InputStream in = new ByteArrayInputStream(pngBinary);
	BufferedImage pngImage = ImageIO.read(in);

	int width = pngImage.getWidth(), height = pngImage.getHeight();
	BufferedImage jpgImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

	Graphics2D g = jpgImage.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
	g.setBackground(Color.WHITE);
	g.clearRect(0, 0, width, height);
	g.drawImage(pngImage, 0, 0, width, height, null);
	g.dispose();

	final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	writer.setOutput(ImageIO.createImageOutputStream(baos));
	writer.write(null, new IIOImage(jpgImage, null, null), JPEG_PARAMS);

	String jpgBase64 = DatatypeConverter.printBase64Binary(baos.toByteArray());
	return jpgBase64;
}
 
Example 2
Project: JuniperBotJ   File: BoxBlurFilter.java   View Source Code Vote up 7 votes
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();

    if (dst == null)
        dst = createCompatibleDestImage(src, null);

    int[] inPixels = new int[width * height];
    int[] outPixels = new int[width * height];
    getRGB(src, 0, 0, width, height, inPixels);

    blur(inPixels, outPixels, width, height, radius);
    blur(outPixels, inPixels, height, width, radius);

    setRGB(dst, 0, 0, width, height, inPixels);
    return dst;
}
 
Example 3
Project: VTerminal   File: Shader.java   View Source Code Vote up 6 votes
/**
 * Swaps two colors on an image.
 *
 * @param image
 *          The image.
 *
 * @param oldColor
 *          The color to swap out.
 *
 * @param newColor
 *          The color to swap in.
 *
 * @return
 *          The result image.
 *
 * @throws NullPointerException
 *           If the image or either color is null.
 */
default BufferedImage swapColor(final @NonNull BufferedImage image, final @NonNull Color oldColor, final @NonNull Color newColor) {
    final BufferedImage result = ImageCache.cloneImage(image);

    final int newRGB = newColor.getRGB();
    final int oldRGB = oldColor.getRGB();

    for (int y = 0; y < result.getHeight(); y++) {
        for (int x = 0; x < result.getWidth(); x++) {
            int pixel = result.getRGB(x, y);

            if (pixel == oldRGB) {
                result.setRGB(x, y, newRGB);
            }
        }
    }

    return result;
}
 
Example 4
Project: openjdk-jdk10   File: ImageTool.java   View Source Code Vote up 6 votes
/**
 * Increases image.
 *
 * @param image an image to enlarge.
 * @param zoom A scale.
 * @return a result image.
 */
public static BufferedImage enlargeImage(BufferedImage image, int zoom) {
    int wight = image.getWidth();
    int height = image.getHeight();
    BufferedImage result = new BufferedImage(wight * zoom,
            height * zoom,
            image.getType());
    int rgb;
    for (int x = 0; x < wight; x++) {
        for (int y = 0; y < height; y++) {
            rgb = image.getRGB(x, y);
            for (int i = 0; i < zoom; i++) {
                for (int j = 0; j < zoom; j++) {
                    result.setRGB(x * zoom + i,
                            y * zoom + j,
                            rgb);
                }
            }
        }
    }
    return result;
}
 
Example 5
Project: GIFKR   File: ImageTools.java   View Source Code Vote up 6 votes
public static BufferedImage fitHeight(BufferedImage img, int width, int height, boolean fast){
	img=resize(img, (int)(img.getWidth()*(height/(double)img.getHeight())), height, fast);
	if(img.getHeight()>height){
		img=img.getSubimage((img.getWidth()-width)/2, 0, width, img.getHeight());
	}

	BufferedImage fullImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
	Graphics2D g=(Graphics2D)fullImage.getGraphics();
	if(!fast){
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
	}
	g.drawImage(img, (width-img.getWidth())/2, 0, null);

	g.dispose();

	return fullImage;	
}
 
Example 6
Project: bicycleSharingServer   File: BufferedImageLuminanceSource.java   View Source Code Vote up 6 votes
public BufferedImageLuminanceSource(BufferedImage image, int left,
        int top, int width, int height) {
    super(width, height);

    int sourceWidth = image.getWidth();
    int sourceHeight = image.getHeight();
    if (left + width > sourceWidth || top + height > sourceHeight) {
        throw new IllegalArgumentException(
                "Crop rectangle does not fit within image data.");
    }

    for (int y = top; y < top + height; y++) {
        for (int x = left; x < left + width; x++) {
            if ((image.getRGB(x, y) & 0xFF000000) == 0) {
                image.setRGB(x, y, 0xFFFFFFFF); // = white
            }
        }
    }

    this.image = new BufferedImage(sourceWidth, sourceHeight,
            BufferedImage.TYPE_BYTE_GRAY);
    this.image.getGraphics().drawImage(image, 0, 0, null);
    this.left = left;
    this.top = top;
}
 
Example 7
Project: Ultraino   File: ImagesUtils.java   View Source Code Vote up 6 votes
public static int[][] getARGB( String path ){
    BufferedImage bi;
    try {
        bi = ImageIO.read( new File(path) );
    } catch (IOException ex) {
        Logger.getLogger(ImagesUtils.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
    final int w = bi.getWidth();
    final int h = bi.getHeight();
    
    int[] argb = new int[w * h];
    bi.getRGB(0, 0, w, h, argb, 0, w);
   
    int[][] matrix = new int[w][h];
    int index = 0;
    for(int ih = 0; ih < h; ++ih){
        for(int iw = 0; iw < w; ++iw){
            matrix[iw][ih] = argb[index];
            
            ++index;
        }
    }
    
    return matrix;
}
 
Example 8
Project: jmonkeybuilder   File: JavaFXImageManager.java   View Source Code Vote up 6 votes
@FXThread
private @NotNull Image readJMETexture(final int width, final int height, @NotNull final String externalForm,
                                      @NotNull final Path cacheFile) {

    final Editor editor = Editor.getInstance();
    final AssetManager assetManager = editor.getAssetManager();
    final Texture texture = assetManager.loadTexture(externalForm);

    final BufferedImage textureImage;
    try {
        textureImage = ImageToAwt.convert(texture.getImage(), false, true, 0);
    } catch (final UnsupportedOperationException e) {
        EditorUtil.handleException(LOGGER, this, e);
        return Icons.IMAGE_512;
    }

    final int imageWidth = textureImage.getWidth();
    final int imageHeight = textureImage.getHeight();

    return scaleAndWrite(width, height, cacheFile, textureImage, imageWidth, imageHeight);
}
 
Example 9
Project: openjdk-jdk10   File: ImageComparator.java   View Source Code Vote up 6 votes
boolean compare(BufferedImage dst, BufferedImage gldImage, int x0, int y0,
                int dx, int dy)
{
    int width = gldImage.getWidth();
    int height = gldImage.getHeight();

    if (x0 < 0) x0 = 0;
    if (x0 > width - dx) x0 = width - dx;
    if (y0 < 0) y0 = 0;
    if (y0 > height - dy) y0 = height - dy;

    int c = 0;

    boolean result = true;
    for (int i = x0; i < x0 + dx; i++) {
        for (int j = y0; j < y0 + dy; j++) {
            boolean cmp = compare(dst.getRGB(i-x0,j-y0),
                                  gldImage.getRGB(i,j));
            result = cmp && result;
        }
    }
    return result;
}
 
Example 10
Project: freecol   File: MapViewer.java   View Source Code Vote up 6 votes
/**
 * Draw the unit's image and occupation indicator in one JLabel object.
 *
 * @param unit The unit to be drawn
 * @return A JLabel object with the unit's image.
 */
private JLabel createUnitLabel(Unit unit) {
    final BufferedImage unitImg = lib.getUnitImage(unit);
    final int width = halfWidth + unitImg.getWidth()/2;
    final int height = unitImg.getHeight();

    BufferedImage img = new BufferedImage(width, height,
                                          BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics();

    final int unitX = (width - unitImg.getWidth()) / 2;
    g.drawImage(unitImg, unitX, 0, null);

    Player player = getMyPlayer();
    String text = Messages.message(unit.getOccupationLabel(player, false));
    g.drawImage(lib.getOccupationIndicatorChip(g, unit, text), 0, 0, null);

    final JLabel label = new JLabel(new ImageIcon(img));
    label.setSize(width, height);

    g.dispose();
    return label;
}
 
Example 11
Project: openjdk-jdk10   File: JFIFMarkerSegment.java   View Source Code Vote up 5 votes
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format.  The length written takes the thumbnail
 * width and height into account.  If necessary, the thumbnail
 * is clipped to 255 x 255 and a warning is sent to the writer
 * argument.  Progress updates are sent to the writer argument.
 */
void write(ImageOutputStream ios,
           BufferedImage thumb,
           JPEGImageWriter writer) throws IOException {
    int thumbWidth = 0;
    int thumbHeight = 0;
    int thumbLength = 0;
    int [] thumbData = null;
    if (thumb != null) {
        // Clip if necessary and get the data in thumbData
        thumbWidth = thumb.getWidth();
        thumbHeight = thumb.getHeight();
        if ((thumbWidth > MAX_THUMB_WIDTH)
            || (thumbHeight > MAX_THUMB_HEIGHT)) {
            writer.warningOccurred(JPEGImageWriter.WARNING_THUMB_CLIPPED);
        }
        thumbWidth = Math.min(thumbWidth, MAX_THUMB_WIDTH);
        thumbHeight = Math.min(thumbHeight, MAX_THUMB_HEIGHT);
        thumbData = thumb.getRaster().getPixels(0, 0,
                                                thumbWidth, thumbHeight,
                                                (int []) null);
        thumbLength = thumbData.length;
    }
    length = DATA_SIZE + LENGTH_SIZE + thumbLength;
    writeTag(ios);
    byte [] id = {0x4A, 0x46, 0x49, 0x46, 0x00};
    ios.write(id);
    ios.write(majorVersion);
    ios.write(minorVersion);
    ios.write(resUnits);
    write2bytes(ios, Xdensity);
    write2bytes(ios, Ydensity);
    ios.write(thumbWidth);
    ios.write(thumbHeight);
    if (thumbData != null) {
        writer.thumbnailStarted(0);
        writeThumbnailData(ios, thumbData, writer);
        writer.thumbnailComplete();
    }
}
 
Example 12
Project: jkes   File: ForkBlur.java   View Source Code Vote up 5 votes
public static BufferedImage blur(BufferedImage srcImage) {
    int w = srcImage.getWidth();
    int h = srcImage.getHeight();

    int[] src = srcImage.getRGB(0, 0, w, h, null, 0, w);
    int[] dst = new int[src.length];

    System.out.println("Array size is " + src.length);
    System.out.println("Threshold is " + sThreshold);

    int processors = Runtime.getRuntime().availableProcessors();
    System.out.println(Integer.toString(processors) + " processor"
            + (processors != 1 ? "s are " : " is ")
            + "available");

    ForkBlur fb = new ForkBlur(src, 0, src.length, dst);

    ForkJoinPool pool = new ForkJoinPool();

    long startTime = System.currentTimeMillis();
    pool.invoke(fb);
    long endTime = System.currentTimeMillis();

    System.out.println("Image blur took " + (endTime - startTime) +
            " milliseconds.");

    BufferedImage dstImage =
            new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    dstImage.setRGB(0, 0, w, h, dst, 0, w);

    return dstImage;
}
 
Example 13
Project: LowPolyWater   File: IconLoader.java   View Source Code Vote up 5 votes
public static ByteBuffer convertToByteBuffer(BufferedImage image) {
	byte[] buffer = new byte[image.getWidth() * image.getHeight() * 4];
	int counter = 0;
	for (int i = 0; i < image.getHeight(); i++)
		for (int j = 0; j < image.getWidth(); j++) {
			int colorSpace = image.getRGB(j, i);
			buffer[counter + 0] = (byte) ((colorSpace << 8) >> 24);
			buffer[counter + 1] = (byte) ((colorSpace << 16) >> 24);
			buffer[counter + 2] = (byte) ((colorSpace << 24) >> 24);
			buffer[counter + 3] = (byte) (colorSpace >> 24);
			counter += 4;
		}
	return ByteBuffer.wrap(buffer);
}
 
Example 14
Project: jmt   File: EnlargeQQPlot.java   View Source Code Vote up 5 votes
BufferedImage convertType(BufferedImage src, int targetType) {
	if (src.getType() == targetType) {
		return src;
	}
	BufferedImage tgt = new BufferedImage(src.getWidth(), src.getHeight(), targetType);
	Graphics2D g = tgt.createGraphics();
	g.drawRenderedImage(src, null);
	g.dispose();
	return tgt;
}
 
Example 15
Project: MapAnalyst   File: ImageUtils.java   View Source Code Vote up 5 votes
/**
 * Converts an image to an optimized version for the default screen.<br>
 * The optimized image should draw faster.
 * @param image The image to optimize.
 * @return Returns the optimized image.
 */
public static BufferedImage optimizeForGraphicsHardware(BufferedImage image) {
    try {
        // create an empty optimized BufferedImage
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        int w = image.getWidth();
        int h = image.getHeight();

        // strange things happen on Linux and Windows on some systems when
        // monitors are set to 16 bit.
        boolean bit16 = gc.getColorModel().getPixelSize() < 24;
        final int transp;
        if (bit16) {
            transp = Transparency.TRANSLUCENT;
        } else {
            transp = image.getColorModel().getTransparency();
        }
        BufferedImage optimized = gc.createCompatibleImage(w, h, transp);

        // draw the passed image into the optimized image
        optimized.getGraphics().drawImage(image, 0, 0, null);
        return optimized;
    } catch (Exception e) {
        // return the original image if an exception occured.
        return image;
    }
}
 
Example 16
Project: jmt   File: EnlargePlotDistGraph.java   View Source Code Vote up 5 votes
BufferedImage convertType(BufferedImage src, int targetType) {
	if (src.getType() == targetType) {
		return src;
	}
	BufferedImage tgt = new BufferedImage(src.getWidth(), src.getHeight(), targetType);
	Graphics2D g = tgt.createGraphics();
	g.drawRenderedImage(src, null);
	g.dispose();
	return tgt;
}
 
Example 17
Project: trashjam2017   File: TrueTypeFont.java   View Source Code Vote up 4 votes
/**
 * Create and store the font
 * 
 * @param customCharsArray Characters that should be also added to the cache.
 */
private void createSet( char[] customCharsArray ) {
	// If there are custom chars then I expand the font texture twice		
	if	(customCharsArray != null && customCharsArray.length > 0) {
		textureWidth *= 2;
	}
	
	// In any case this should be done in other way. Texture with size 512x512
	// can maintain only 256 characters with resolution of 32x32. The texture
	// size should be calculated dynamicaly by looking at character sizes. 
	
	try {
		
		BufferedImage imgTemp = new BufferedImage(textureWidth, textureHeight, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = (Graphics2D) imgTemp.getGraphics();

		g.setColor(new Color(255,255,255,1));
		g.fillRect(0,0,textureWidth,textureHeight);
		
		int rowHeight = 0;
		int positionX = 0;
		int positionY = 0;
		
		int customCharsLength = ( customCharsArray != null ) ? customCharsArray.length : 0; 

		for (int i = 0; i < 256 + customCharsLength; i++) {
			
			// get 0-255 characters and then custom characters
			char ch = ( i < 256 ) ? (char) i : customCharsArray[i-256];
			
			BufferedImage fontImage = getFontImage(ch);

			IntObject newIntObject = new IntObject();

			newIntObject.width = fontImage.getWidth();
			newIntObject.height = fontImage.getHeight();

			if (positionX + newIntObject.width >= textureWidth) {
				positionX = 0;
				positionY += rowHeight;
				rowHeight = 0;
			}

			newIntObject.storedX = positionX;
			newIntObject.storedY = positionY;

			if (newIntObject.height > fontHeight) {
				fontHeight = newIntObject.height;
			}

			if (newIntObject.height > rowHeight) {
				rowHeight = newIntObject.height;
			}

			// Draw it here
			g.drawImage(fontImage, positionX, positionY, null);

			positionX += newIntObject.width;

			if( i < 256 ) { // standard characters
				charArray[i] = newIntObject;
			} else { // custom characters
				customChars.put( new Character( ch ), newIntObject );
			}

			fontImage = null;
		}

		fontTexture = BufferedImageUtil
				.getTexture(font.toString(), imgTemp);

	} catch (IOException e) {
		System.err.println("Failed to create font.");
		e.printStackTrace();
	}
}
 
Example 18
Project: BaseClient   File: TextureAnimations.java   View Source Code Vote up 4 votes
private static byte[] loadImage(String p_loadImage_0_, int p_loadImage_1_)
{
    GameSettings gamesettings = Config.getGameSettings();

    try
    {
        ResourceLocation resourcelocation = new ResourceLocation(p_loadImage_0_);
        InputStream inputstream = Config.getResourceStream(resourcelocation);

        if (inputstream == null)
        {
            return null;
        }
        else
        {
            BufferedImage bufferedimage = readTextureImage(inputstream);
            inputstream.close();

            if (bufferedimage == null)
            {
                return null;
            }
            else
            {
                if (p_loadImage_1_ > 0 && bufferedimage.getWidth() != p_loadImage_1_)
                {
                    double d0 = (double)(bufferedimage.getHeight() / bufferedimage.getWidth());
                    int j = (int)((double)p_loadImage_1_ * d0);
                    bufferedimage = scaleBufferedImage(bufferedimage, p_loadImage_1_, j);
                }

                int k2 = bufferedimage.getWidth();
                int i = bufferedimage.getHeight();
                int[] aint = new int[k2 * i];
                byte[] abyte = new byte[k2 * i * 4];
                bufferedimage.getRGB(0, 0, k2, i, aint, 0, k2);

                for (int k = 0; k < aint.length; ++k)
                {
                    int l = aint[k] >> 24 & 255;
                    int i1 = aint[k] >> 16 & 255;
                    int j1 = aint[k] >> 8 & 255;
                    int k1 = aint[k] & 255;

                    if (gamesettings != null && gamesettings.anaglyph)
                    {
                        int l1 = (i1 * 30 + j1 * 59 + k1 * 11) / 100;
                        int i2 = (i1 * 30 + j1 * 70) / 100;
                        int j2 = (i1 * 30 + k1 * 70) / 100;
                        i1 = l1;
                        j1 = i2;
                        k1 = j2;
                    }

                    abyte[k * 4 + 0] = (byte)i1;
                    abyte[k * 4 + 1] = (byte)j1;
                    abyte[k * 4 + 2] = (byte)k1;
                    abyte[k * 4 + 3] = (byte)l;
                }

                return abyte;
            }
        }
    }
    catch (FileNotFoundException var18)
    {
        return null;
    }
    catch (Exception exception)
    {
        exception.printStackTrace();
        return null;
    }
}
 
Example 19
Project: geomapapp   File: Grid2D.java   View Source Code Vote up 4 votes
public Image getGeoImage() {
	if( projection instanceof RectangularProjection ) return this;
//	System.out.println( projection.getClass().getName() );
	if( !projection.isCylindrical() )return null;
//	CylindricalProjection proj = (CylindricalProjection)projection;
	double[] wesn = getWESN();
	double dy = (wesn[3]-wesn[2])/(grid.getHeight()-1.);
	BufferedImage im = new BufferedImage(grid.getWidth(), grid.getHeight(), grid.getType());
	for( int j=0 ; j<grid.getHeight() ; j++) {
		double y = projection.getMapXY(new Point2D.Double(wesn[0],wesn[3] - j*dy)).getY();
		double d = y-Math.floor(y);
		int iy = (int)Math.floor(y) - bounds.y;
		if( iy==grid.getHeight()-1 )iy--;
		for( int x=0 ; x<grid.getWidth() ; x++ ) {
			if( iy<0 || iy>grid.getHeight()-2 ) {
				im.setRGB(x,j,0);
				continue;
			}
			int rgb1 = grid.getRGB(x,iy);
			int rgb2 = grid.getRGB(x,iy+1);
			int c1 = (rgb1>>24)&255;
			int c2 = (rgb2>>24)&255;
			if( c1!=255 || c2!=255 ) {
				im.setRGB(x,j,0);
				continue;
			}
			int rgb = 0xff000000;
			c1 = (rgb1>>16)&255;
			c2 = (rgb2>>16)&255;
			int c = (int)(c1+d*(c2-c1));
			if( c<0 )c=0;
			else if( c>255 )c=255;
			rgb |= c<<16;
			c1 = (rgb1>>8)&255;
			c2 = (rgb2>>8)&255;
			c = (int)(c1+d*(c2-c1));
			if( c<0 )c=0;
			else if( c>255 )c=255;
			rgb |= c<<8;
			c1 = rgb1&255;
			c2 = rgb2&255;
			c = (int)(c1+d*(c2-c1));
			if( c<0 )c=0;
			else if( c>255 )c=255;
			rgb |= c;
			im.setRGB( x, j, rgb);
		}
	}
	Grid2D.Image g = new Grid2D.Image( new Rectangle(0, 0, im.getWidth(), im.getHeight()),
				new RectangularProjection( wesn, im.getWidth(), im.getHeight()),
				im );
	return g;
}
 
Example 20
Project: Backmemed   File: DynamicTexture.java   View Source Code Vote up 4 votes
public DynamicTexture(BufferedImage bufferedImage)
{
    this(bufferedImage.getWidth(), bufferedImage.getHeight());
    bufferedImage.getRGB(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), this.dynamicTextureData, 0, bufferedImage.getWidth());
    this.updateDynamicTexture();
}