本文整理汇总了Java中javafx.scene.image.PixelReader.getPixels方法的典型用法代码示例。如果您正苦于以下问题:Java PixelReader.getPixels方法的具体用法?Java PixelReader.getPixels怎么用?Java PixelReader.getPixels使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javafx.scene.image.PixelReader
的用法示例。
在下文中一共展示了PixelReader.getPixels方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resizeCanvas
import javafx.scene.image.PixelReader; //导入方法依赖的package包/类
public static Image resizeCanvas(Image imageSource, int newWidth, int newHeight, int offsetX, int offsetY) {
int sourceWidth = (int) imageSource.getWidth();
int sourceHeight = (int) imageSource.getHeight();
// No work needed here...
if (sourceWidth == newWidth && sourceHeight == newHeight)
return imageSource;
WritableImage outputImage = new WritableImage(newWidth, newHeight);
PixelReader pixelReader = imageSource.getPixelReader();
PixelWriter pixelWriter = outputImage.getPixelWriter();
WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();
int[] buffer = new int[sourceWidth * sourceHeight];
pixelReader.getPixels(0, 0, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth);
pixelWriter.setPixels(offsetX, offsetY, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth);
return outputImage;
}
示例2: getPixels
import javafx.scene.image.PixelReader; //导入方法依赖的package包/类
@Override
public int[] getPixels(Image img, int x, int y, int w, int h)
{
int[] pixels = new int[w * h];
PixelReader reader = img.getPixelReader();
PixelFormat.Type type = reader.getPixelFormat().getType();
WritablePixelFormat<IntBuffer> format = null;
if(type == PixelFormat.Type.INT_ARGB_PRE)
{
format = PixelFormat.getIntArgbPreInstance();
}
else
{
format = PixelFormat.getIntArgbInstance();
}
reader.getPixels(x, y, w, h, format, pixels, 0, w);
return pixels;
}
示例3: readBlockColor
import javafx.scene.image.PixelReader; //导入方法依赖的package包/类
@Test
public void readBlockColor(){
ColorComponentProvider provider = getColorComponentProvider();
Image testImage = getImage();
PixelReader reader = testImage.getPixelReader();
assertFalse("Open testing image", testImage.isError());
int[] buf = new int[256 * 256];
reader.getPixels(0, 0, 256, 256, PixelFormat.getIntArgbInstance(), buf, 0, 256);
int samePixelsCounter = 0;
for (int i = 1; i < testImage.getHeight() - 1; i++) {
for (int j = 1; j < testImage.getWidth() - 1; j++) {
if(provider.getComponent(buf[i * 256 + j]) ==
provider.getComponent(buf[(i + 1) * 256 + j]))
samePixelsCounter++;
testGradientImage(
provider.getComponent(buf[i * 256 + j]),
provider.getComponent(buf[i * 256 + (j - 1)]),
provider.getComponent(buf[(i + 1) * 256 + j]),
provider.getComponent(buf[i * 256 + (j - 1)]),
provider.getComponent(buf[i * 256 + (j + 1)]));
}
}
assertTrue("More than half of the pixels the same color.",
samePixelsCounter < 256 * 256 / 2);
}
示例4: fromFXImage
import javafx.scene.image.PixelReader; //导入方法依赖的package包/类
/**
* Snapshots the specified JavaFX {@link Image} object and stores a copy of
* its pixels into a {@link BufferedImage} object, creating a new object if
* needed. The method will only convert a JavaFX {@code Image} that is
* readable as per the conditions on the {@link Image#getPixelReader()
* Image.getPixelReader()} method. If the {@code Image} is not readable, as
* determined by its {@code getPixelReader()} method, then this method will
* return null. If the {@code Image} is a writable, or other dynamic image,
* then the {@code BufferedImage} will only be set to the current state of
* the pixels in the image as determined by its {@link PixelReader}. Further
* changes to the pixels of the {@code Image} will not be reflected in the
* returned {@code BufferedImage}.
* <p>
* The optional {@code BufferedImage} parameter may be reused to store the
* copy of the pixels. A new {@code BufferedImage} will be created if the
* supplied object is null, is too small or of a type which the image pixels
* cannot be easily converted into.
*
* @param img
* the JavaFX {@code Image} to be converted
* @param bimg
* an optional {@code BufferedImage} object that may be used to
* store the returned pixel data
* @return a {@code BufferedImage} containing a snapshot of the JavaFX
* {@code Image}, or null if the {@code Image} is not readable.
* @since JavaFX 2.2
*/
public static BufferedImage fromFXImage(Image img, BufferedImage bimg) {
PixelReader pr = img.getPixelReader();
if (pr == null) {
return null;
}
int iw = (int) img.getWidth();
int ih = (int) img.getHeight();
int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg);
if (bimg != null) {
int bw = bimg.getWidth();
int bh = bimg.getHeight();
if (bw < iw || bh < ih || bimg.getType() != prefBimgType) {
bimg = null;
} else if (iw < bw || ih < bh) {
Graphics2D g2d = bimg.createGraphics();
g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, bw, bh);
g2d.dispose();
}
}
if (bimg == null) {
bimg = new BufferedImage(iw, ih, prefBimgType);
}
IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
int offset = icr.getDataOffset(0);
int scan = icr.getScanlineStride();
int data[] = icr.getDataStorage();
WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg);
pr.getPixels(0, 0, iw, ih, pf, data, offset, scan);
return bimg;
}
示例5: pixelate
import javafx.scene.image.PixelReader; //导入方法依赖的package包/类
private void pixelate(final DotMatrix MATRIX, final boolean WITH_RED, final boolean WITH_GREEN, final boolean WITH_BLUE) {
PixelReader reader = src.getPixelReader();
PixelWriter writer = dst.getPixelWriter();
WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();
int width = (int) src.getWidth();
int height = (int) src.getHeight();
int kernelSize = 6;
for (int y = kernelSize; y < height - kernelSize * 2; y += kernelSize * 2 + 1) {
for (int x = kernelSize; x < width - kernelSize * 2; x += kernelSize * 2 + 1) {
int kernelWidth = kernelSize * 2 + 1;
int kernelHeight = kernelSize * 2 + 1;
int[] buffer = new int[kernelWidth * kernelHeight];
reader.getPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);
int alpha = 0;
int red = 0;
int green = 0;
int blue = 0;
for (int color : buffer) {
alpha += (color >>> 24);
red += (color >>> 16 & 0xFF);
green += (color >>> 8 & 0xFF);
blue += (color & 0xFF);
}
alpha = alpha / kernelWidth / kernelHeight;
red = WITH_RED ? red / kernelWidth / kernelHeight : 0;
green = WITH_GREEN ? green / kernelWidth / kernelHeight : 0;
blue = WITH_BLUE ? blue / kernelWidth / kernelHeight : 0;
int pixelateColor = (alpha << 24) + (red << 16) + (green << 8) + blue;
Arrays.fill(buffer, pixelateColor);
writer.setPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);
MATRIX.setPixel(x / kernelWidth, y / kernelHeight, pixelateColor);
}
}
}
示例6: pixelate
import javafx.scene.image.PixelReader; //导入方法依赖的package包/类
private void pixelate() {
PixelReader reader = src.getPixelReader();
PixelWriter writer = dst.getPixelWriter();
WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();
int width = (int) src.getWidth();
int height = (int) src.getHeight();
// Bird Demo
// int kernelSize = 4;
// Han Solo Demo
int kernelSize = 6;
for (int y = kernelSize; y < height - kernelSize * 2; y += kernelSize * 2 + 1) {
for (int x = kernelSize; x < width - kernelSize * 2; x += kernelSize * 2 + 1) {
int kernelWidth = kernelSize * 2 + 1;
int kernelHeight = kernelSize * 2 + 1;
int[] buffer = new int[kernelWidth * kernelHeight];
reader.getPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);
int alpha = 0;
int red = 0;
int green = 0;
int blue = 0;
for (int color : buffer) {
alpha += (color >>> 24);
red += (color >>> 16 & 0xFF);
green += (color >>> 8 & 0xFF);
blue += (color & 0xFF);
}
alpha = alpha / kernelWidth / kernelHeight;
red = red / kernelWidth / kernelHeight;
green = green / kernelWidth / kernelHeight;
blue = blue / kernelWidth / kernelHeight;
int pixelateColor = (alpha << 24) + (red << 16) + (green << 8) + blue;
Arrays.fill(buffer, pixelateColor);
writer.setPixels(x, y, kernelWidth, kernelHeight, format, buffer, 0, kernelWidth);
matrix.setPixel(x / kernelWidth, y / kernelHeight, pixelateColor);
}
}
addText();
}