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

The following are Jave code examples for showing how to use getType() 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: GIFKR   File: PerspectiveFilter.java   View Source Code Vote up 6 votes
@Override
protected BufferedImage apply(BufferedImage img) {
	
	BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
	Graphics2D g = img2.createGraphics();
	
	for(int y = 0; y < img.getHeight(); y++) {
		BufferedImage row = img.getSubimage(0, y, img.getWidth(), 1);
		
		int offset = Math.round((y/2f - img.getHeight()/4f) * (2 * shift * img.getWidth()));
		offset = offset % img.getWidth();
		
		g.drawImage(row, offset, y, null);
		g.drawImage(row, (offset < 0 ? 1 : -1) * img.getWidth() + offset, y, null);
	}

	g.dispose();
	
	return img2;
}
 
Example 2
Project: geomapapp   File: GridTileLayer.java   View Source Code Vote up 6 votes
/**
 *	Takes an Buffered Image and resizes it to TILE_SIZE x TILE_SIZE 
 */
private static BufferedImage resizeImage(BufferedImage sample, double sampleMinLat, double sampleMaxLat, double tileMinLat, double tileMaxLat) {
	BufferedImage img = new BufferedImage(TILE_SIZE, TILE_SIZE, sample.getType());
	double tileScale = TILE_SIZE / (tileMinLat - tileMaxLat);

	// s for source; d for destination; all measurements in pixels
	int sx1 = 0;
	int sy1 = 0;
	int sx2 = sample.getWidth();
	int sy2 = sample.getHeight();

	int dx1 = 0;
	int dy1 = (int) ((sampleMaxLat - tileMaxLat) * tileScale);
	int dx2 = TILE_SIZE;
	int dy2 = (int) ((sampleMinLat - tileMaxLat) * tileScale);

	img.createGraphics().drawImage(sample, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);

	return img;
}
 
Example 3
Project: StreamDeckCore   File: IconHelper.java   View Source Code Vote up 6 votes
public static BufferedImage rotate180(BufferedImage inputImage) {
	// The most of code is same as before
	int width = inputImage.getWidth();
	int height = inputImage.getHeight();
	BufferedImage returnImage = new BufferedImage(height, width, inputImage.getType());
	// We have to change the width and height because when you rotate the
	// image by 90 degree, the
	// width is height and height is width <img
	// src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/smile.png'
	// class='bbc_emoticon' alt=':)' />

	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++) {
			int nX = height - x - 1;
			int nY = height - (width - y - 1) - 1;
			returnImage.setRGB(nX, nY, inputImage.getRGB(x, y));
		}
	}
	return returnImage;
}
 
Example 4
Project: openjdk-jdk10   File: EffectUtils.java   View Source Code Vote up 6 votes
/**
 * <p>Writes a rectangular area of pixels in the destination
 * <code>BufferedImage</code>. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the destination image
 * @param x the x location at which to start storing pixels
 * @param y the y location at which to start storing pixels
 * @param w the width of the rectangle of pixels to store
 * @param h the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
 
Example 5
Project: GameResourceBot   File: Preprocessor.java   View Source Code Vote up 6 votes
private static BufferedImage resizeSource(BufferedImage image) {
    double factor = 720d / image.getWidth();
    int scaledHeight = (int) (image.getHeight() * factor);
    int scaledWidth = (int) (image.getWidth() * factor);

    Image tmp = image.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
    int type = image.getType();
    if (type == 0)
        type = 5; // hacky but does work

    BufferedImage dimg = new BufferedImage(scaledWidth, scaledHeight, type);
    Graphics2D g2d = dimg.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();
    return dimg;
}
 
Example 6
Project: movie-db-java-on-azure   File: Function.java   View Source Code Vote up 6 votes
private static byte[] resizeImage(byte[] imageInByte) throws IOException {
    InputStream in = new ByteArrayInputStream(imageInByte);
    BufferedImage originalImage = ImageIO.read(in);

    final int imageWidth = 60;
    final int imageHeight = imageWidth * originalImage.getHeight() / originalImage.getWidth();

    BufferedImage resizedImage = new BufferedImage(imageWidth, imageHeight, originalImage.getType());
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, imageWidth, imageHeight, null);
    g.dispose();

    byte[] imageInByteOut = null;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        ImageIO.write(resizedImage, "png", out);
        out.flush();
        imageInByteOut = out.toByteArray();
    }

    return imageInByteOut;
}
 
Example 7
Project: jdk8u-jdk   File: OpCompatibleImageTest.java   View Source Code Vote up 6 votes
public void doTest(int type) {
    System.out.println("Test for type " + describeType(type));

    BufferedImage src = createTestImage(type);

    BufferedImage res = null;

    System.out.println("Testing null destination...");
    try {
        res = op.filter(src, null);
    } catch (ImagingOpException e) {
        throw new RuntimeException("Test FAILED!", e);
    }

    if (res == null ||
        ((src.getType() != BufferedImage.TYPE_BYTE_INDEXED) &&
         (res.getType() != src.getType())))
    {
        throw new RuntimeException("Test FAILED!");
    }
    System.out.println("Test PASSED.");
}
 
Example 8
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 9
Project: openjdk-jdk10   File: EffectUtils.java   View Source Code Vote up 6 votes
/**
 * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
 * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
 * will unmanage the image.</p>
 *
 * @param img    the destination image
 * @param x      the x location at which to start storing pixels
 * @param y      the y location at which to start storing pixels
 * @param w      the width of the rectangle of pixels to store
 * @param h      the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, byte[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }
    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 10
Project: geomapapp   File: ImageResampler.java   View Source Code Vote up 5 votes
public BufferedImage resampleImage(BufferedImage sample, double sampleMinLat, double sampleMaxLat, double tileMinLat, double tileMaxLat, int tileSize) {
 			BufferedImage img = new BufferedImage(sample.getWidth(), tileSize, sample.getType());
			double tileScale_DegreesPerPixel = (tileMinLat - tileMaxLat) / tileSize;

			// Y = 0 is equator
			double sampleMinY = Math.log( Math.tan(Math.toRadians(sampleMinLat)) + 
									1 / Math.cos(Math.toRadians(sampleMinLat)) );
			double sampleMaxY = Math.log( Math.tan(Math.toRadians(sampleMaxLat)) + 
									1 / Math.cos(Math.toRadians(sampleMaxLat)) );
			
			double sampleYDelta = sampleMinY - sampleMaxY;

			int rgb[] = new int[img.getWidth()];
			for (int tileRow = 0; tileRow < tileSize; tileRow++) {
				double lat = tileScale_DegreesPerPixel * tileRow + tileMaxLat;
				
				if (lat > 81) continue;
				if (lat < -79) continue;
				
				lat = Math.toRadians(lat);
				
				double sampleY = (Math.log(Math.tan(lat) + 1 / Math.cos(lat)));
				
				double sampleRatio = (sampleY - sampleMaxY) / sampleYDelta;
//				System.out.println(Math.toDegrees(lat) + "\t" + sampleY + "\t" + sampleRatio);
				
				if (sampleRatio > 1 || sampleRatio < 0) {
					System.out.println("Outside sample range");
					continue;
				}
				
				int sampleRow = (int) Math.floor(sampleRatio * sample.getHeight());
				rgb = sample.getRGB(0, sampleRow, sample.getWidth(), 1, rgb, 0, 1);
				img.setRGB(0, tileRow, img.getWidth(), 1, rgb, 0, 1);
			}
			
			return img; 
		}
 
Example 11
Project: QN-ACTR-Release   File: EnlargePDF.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 12
Project: CyberSecurity   File: PictureEdit.java   View Source Code Vote up 5 votes
private BufferedImage copyImage(BufferedImage i)
 {
 	BufferedImage img = new BufferedImage(i.getWidth(), i.getHeight(), i.getType());
	for (int y = 0; y < i.getHeight(); y++)
	{
		for (int x = 0; x < i.getWidth(); x++)
		{
			int rgb = i.getRGB(x, y);
			img.setRGB(x, y, rgb);
		}
	}
return img;
 }
 
Example 13
Project: EmoticonGIFKeyboard   File: Utils.java   View Source Code Vote up 5 votes
static BufferedImage resizeImage(BufferedImage originalImage) {
    int imageType = originalImage.getType();
    if (imageType == 0) imageType = 5;

    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, imageType);

    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();

    return resizedImage;
}
 
Example 14
Project: opentest   File: ImageUtil.java   View Source Code Vote up 5 votes
/**
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 *
 * @param srcImg - Source image to scale
 * @param width - Desired width
 * @param height - Desired height
 * @return - The new resized image
 */
public static BufferedImage scaleImage(BufferedImage srcImg, int width, int height) {
    BufferedImage resizedImg = new BufferedImage(width, height, srcImg.getType());
    Graphics2D g2 = resizedImg.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g2.drawImage(srcImg, 0, 0, width, height, null);
    g2.dispose();

    return resizedImg;
}
 
Example 15
Project: imagetozxspec   File: ImageHelper.java   View Source Code Vote up 5 votes
/**
 * Prepares the main preview window image prior to rendering (puts the preprocessed image left, processed
 * image right, does any scaling necessary to fit the screen)
 *
 * @param preprocessed the preprocessed image
 * @param result the result image
 * @param  fpsString the current processing frame rate
 * @return the image ready to render in the main window
 */
public static BufferedImage prepareMainPreview(BufferedImage preprocessed, BufferedImage result, float fpsString) {
	BufferedImage left = preprocessed;
	BufferedImage right = result;
	int width = result.getWidth();
	int height = result.getHeight();
	boolean needsScaling = false;
	while (imageOutOfScreenBounds(width*2, height)) {
		width /=2;
		height /=2;
		needsScaling = true;
	}
	if (needsScaling) {
		left = ImageHelper.quickScaleImage(preprocessed, width, height);
		right = ImageHelper.smoothScaleImage(result, width, height); // Smooth scale needed in order to maintain dither patterns in preview
	}
	BufferedImage mainPreviewImage = new BufferedImage(width*2, height, result.getType());
	Graphics preBuffer = mainPreviewImage.createGraphics();
	preBuffer.drawImage(left, 0, 0, null);
	preBuffer.drawImage(right, width, 0, null);
	if (OptionsObject.getInstance().getFpsCounter()) {
		preBuffer.setColor(Color.WHITE);
		preBuffer.drawString(getCaption("main_fps_overlay") + " " + fpsString, 10, 20);
	}
	preBuffer.dispose();
	return mainPreviewImage;
}
 
Example 16
Project: StreamDeckCore   File: IconHelper.java   View Source Code Vote up 5 votes
public static BufferedImage fillBackground(BufferedImage img, Color color) {
	BufferedImage nImg = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
	Graphics2D g = nImg.createGraphics();
	g.setColor(color);
	g.fillRect(0, 0, nImg.getWidth(), nImg.getHeight());
	g.drawImage(img, 0, 0, null);
	g.dispose();
	return nImg;
}
 
Example 17
Project: QN-ACTR-Release   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 18
Project: sucok-framework   File: ImageSizeUtils.java   View Source Code Vote up 4 votes
public static BufferedImage cut(BufferedImage bi, int w, int h) throws IOException {
	// 进行图片拉缩
	int oldWidth = bi.getWidth();
	int oldHeight = bi.getHeight();
	double wr = 1.0;
	double hr = 1.0;
	// 横图(宽 > 高)
	if (oldWidth > oldHeight) {
		hr = h * 1.0 / oldHeight;
		wr = hr;
		// 拉伸后宽度不足
		if (wr * oldWidth < w) {
			wr = w * 1.0 / oldWidth;
			hr = wr;
		}
	}
	// 竖图
	else {
		wr = w * 1.0 / oldWidth;
		hr = wr;
		// 拉伸后高度不足
		if (hr * oldHeight < h) {
			hr = h * 1.0 / oldHeight;
			wr = hr;
		}
	}
	AffineTransform transform = AffineTransform.getScaleInstance(wr, hr);
	AffineTransformOp ato = new AffineTransformOp(transform, null);
	BufferedImage image = (BufferedImage) ato.filter(bi, null);
	int newWidth = (int) (wr * oldWidth);
	int newHeight = (int) (hr * oldHeight);
	int x = 0, y = 0;
	// 宽度大于预期宽度,裁剪左右
	if (newWidth > w) {
		x = -(newWidth - w) / 2;
	} else if (newHeight > h) {// 裁剪上下
		y = -(newHeight - h) / 2;
	}
	BufferedImage newImage = new BufferedImage(w, h, image.getType());
	Graphics g = newImage.getGraphics();
	g.drawImage(image, x, y, null);
	return newImage;
}
 
Example 19
Project: java-sense-hat   File: SenseHat.java   View Source Code Vote up 4 votes
public void fadeTo(BufferedImage image, Duration duration) throws IOException {

        if(image.getType() == BufferedImage.TYPE_USHORT_565_RGB) {
            short[] oldData = getDisplayData();
            SenseHatColor[] oldColors = new SenseHatColor[64];

            short[] newData = (short[]) image.getRaster().getDataElements(0, 0, 8, 8, new short[64]);
            SenseHatColor[] newColors = new SenseHatColor[64];

            for (int i = 0; i < 64; i++) {
                oldColors[i] = new SenseHatColor(oldData[i]);
                newColors[i] = new SenseHatColor(newData[i]);
            }

            short[] data = new short[64];

            long sleepTime = Math.max(10, duration.toMillis() / 64);

            float durationInMillis = duration.toMillis();
            long start = System.currentTimeMillis();
            while (true) {
                long currentDuration = System.currentTimeMillis() - start;
                float factor = currentDuration / durationInMillis;

                if (factor < 1d) {
                    for (int i = 0; i < 64; i++) {
                        data[i] = (short) oldColors[i].mix(newColors[i], factor).getSenseHatColor();
                    }
                    setDisplayData(data);
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        log.error(e.getLocalizedMessage(), e);
                    }
                } else {
                    setDisplayData(newData);
                    break;
                }
            }

        } else {
            fadeTo(convertImage(image), duration);
        }
    }
 
Example 20
Project: jdk8u-jdk   File: PathGraphics.java   View Source Code Vote up 4 votes
/**
 * Return true if the BufferedImage argument has non-opaque
 * bits in it and therefore can not be directly rendered by
 * GDI. Return false if the image is opaque. If this function
 * can not tell for sure whether the image has transparent
 * pixels then it assumes that it does.
 */
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
    ColorModel colorModel = bufferedImage.getColorModel();
    boolean hasTransparency = colorModel == null
        ? true
        : colorModel.getTransparency() != ColorModel.OPAQUE;

    /*
     * For the default INT ARGB check the image to see if any pixels are
     * really transparent. If there are no transparent pixels then the
     * transparency of the color model can be ignored.
     * We assume that IndexColorModel images have already been
     * checked for transparency and will be OPAQUE unless they actually
     * have transparent pixels present.
     */
    if (hasTransparency && bufferedImage != null) {
        if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
            bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
            DataBuffer db =  bufferedImage.getRaster().getDataBuffer();
            SampleModel sm = bufferedImage.getRaster().getSampleModel();
            if (db instanceof DataBufferInt &&
                sm instanceof SinglePixelPackedSampleModel) {
                SinglePixelPackedSampleModel psm =
                    (SinglePixelPackedSampleModel)sm;
                // Stealing the data array for reading only...
                int[] int_data =
                    SunWritableRaster.stealData((DataBufferInt) db, 0);
                int x = bufferedImage.getMinX();
                int y = bufferedImage.getMinY();
                int w = bufferedImage.getWidth();
                int h = bufferedImage.getHeight();
                int stride = psm.getScanlineStride();
                boolean hastranspixel = false;
                for (int j = y; j < y+h; j++) {
                    int yoff = j * stride;
                    for (int i = x; i < x+w; i++) {
                        if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
                            hastranspixel = true;
                            break;
                        }
                    }
                    if (hastranspixel) {
                        break;
                    }
                }
                if (hastranspixel == false) {
                    hasTransparency = false;
                }
            }
        }
    }

    return hasTransparency;
}