下面列出了怎么用javafx.scene.image.PixelFormat的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onTick(long ticks) {
Platform.runLater(() -> {
if (VgaConfigures.getResolution() == VgaConfigures.Resolution.CLOSE) {
return;
}
if (machine.isLooping() && screen.getImage() != content) {
screen.setImage(content);
}
ScreenProvider currentProvider = VgaConfigures.isTextMode() ? textProvider : graphProvider;
content.getPixelWriter().setPixels(0, 0, WIDTH, HEIGHT, PixelFormat.getByteBgraPreInstance()
, currentProvider.get(), 0, WIDTH * 4);
// blink cursor
if (ticks % 10 == 0) {
machine.blink();
}
});
}
public static void initalizeImage() {
noisePixels = ByteBuffer.allocate(w * h * 4);
noiseBuffer = new PixelBuffer<>(w, h, noisePixels, PixelFormat.getByteBgraPreInstance());
testimage = new WritableImage(noiseBuffer);
final Canvas noiseCanvas = new Canvas(w, h);
final GraphicsContext noiseContext = noiseCanvas.getGraphicsContext2D();
final byte[] randomArray = new byte[w * h * 4];
new Random().nextBytes(randomArray);
noiseContext.getPixelWriter().setPixels(0, 0, w, h, PixelFormat.getByteBgraInstance(), randomArray, 0, w);
noiseCanvas.snapshot(null, testimage);
final Canvas easyCanvas = new Canvas(w2, h2);
final GraphicsContext easyContext = easyCanvas.getGraphicsContext2D();
easyContext.setStroke(Color.BLUE);
easyContext.strokeOval(20, 30, 40, 50);
easyContext.setStroke(Color.RED);
easyContext.strokeOval(30, 40, 50, 60);
easyContext.setStroke(Color.GREEN);
easyContext.strokeOval(40, 50, 60, 70);
easyContext.setStroke(Color.ORANGE);
easyContext.strokeRect(0, 0, w2, h2);
testimage2 = easyCanvas.snapshot(null, null);
initialized.set(true);
}
@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;
}
/**
* Determine the appropriate {@link WritablePixelFormat} type that can be
* used to transfer data into the indicated BufferedImage.
*
* @param bimg
* the BufferedImage that will be used as a destination for a
* {@code PixelReader<IntBuffer>#getPixels()} operation.
* @return
*/
private static WritablePixelFormat<IntBuffer> getAssociatedPixelFormat(BufferedImage bimg) {
switch (bimg.getType()) {
// We lie here for xRGB, but we vetted that the src data was opaque
// so we can ignore the alpha. We use ArgbPre instead of Argb
// just to get a loop that does not have divides in it if the
// PixelReader happens to not know the data is opaque.
case BufferedImage.TYPE_INT_RGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
return PixelFormat.getIntArgbPreInstance();
case BufferedImage.TYPE_INT_ARGB:
return PixelFormat.getIntArgbInstance();
default:
// Should not happen...
throw new InternalError("Failed to validate BufferedImage type");
}
}
protected WritableImage convertDataArrayToImage(final double[] inputData, final int dataWidth, final int dataHeight,
final ColorGradient colorGradient) {
final int length = dataWidth * dataHeight;
final byte[] byteBuffer = ByteArrayCache.getInstance().getArrayExact(length * BGRA_BYTE_SIZE);
final int rowSizeInBytes = BGRA_BYTE_SIZE * dataWidth;
final WritableImage image = this.getImage(dataWidth, dataHeight);
final PixelWriter pixelWriter = image.getPixelWriter();
if (pixelWriter == null) {
if (LOGGER.isErrorEnabled()) {
LOGGER.atError().log("Could not get PixelWriter for image");
}
return image;
}
final int hMinus1 = dataHeight - 1;
for (int yIndex = 0; yIndex < dataHeight; yIndex++) {
final int rowIndex = dataWidth * yIndex;
final int rowPixelIndex = rowSizeInBytes * (hMinus1 - yIndex);
for (int xIndex = 0; xIndex < dataWidth; xIndex++) {
final int x = xIndex;
final int[] color = colorGradient.getColorBytes(inputData[rowIndex + xIndex]);
final int pixelIndex = rowPixelIndex + x * BGRA_BYTE_SIZE;
byteBuffer[pixelIndex] = (byte) (color[3]);
byteBuffer[pixelIndex + 1] = (byte) (color[2]);
byteBuffer[pixelIndex + 2] = (byte) (color[1]);
byteBuffer[pixelIndex + 3] = (byte) (color[0]);
}
}
pixelWriter.setPixels(0, 0, dataWidth, dataHeight, PixelFormat.getByteBgraPreInstance(), byteBuffer, 0,
rowSizeInBytes);
ByteArrayCache.getInstance().add(byteBuffer);
return image;
}
@Start
public void setUp(@SuppressWarnings("unused") final Stage stage) {
int w = 200;
int h = 300;
// generate test image with simple shapes
final Canvas originalCanvas = new Canvas(w, h);
final GraphicsContext originalContext = originalCanvas.getGraphicsContext2D();
originalContext.setStroke(Color.BLUE);
originalContext.strokeOval(20, 30, 40, 50);
originalContext.setStroke(Color.RED);
originalContext.strokeOval(30, 40, 50, 60);
originalContext.setStroke(Color.GREEN);
originalContext.strokeOval(40, 50, 60, 70);
originalContext.setStroke(Color.ORANGE);
originalContext.strokeRect(0, 0, w, h);
imageOvals = originalCanvas.snapshot(null, null);
//generate test image with noise data (not very compressible)
final Canvas noiseCanvas = new Canvas(600, 333);
final GraphicsContext noiseContext = noiseCanvas.getGraphicsContext2D();
byte[] randomArray = new byte[600 * 333 * 4];
new Random().nextBytes(randomArray);
noiseContext.getPixelWriter().setPixels(0, 0, 600, 333, PixelFormat.getByteBgraInstance(), randomArray, 0, 600);
imageRandom = noiseCanvas.snapshot(null, null);
// generate test image with minimal dimensions
final Canvas onexoneCanvas = new Canvas(1, 1);
final GraphicsContext onexoneContext = onexoneCanvas.getGraphicsContext2D();
onexoneContext.getPixelWriter().setArgb(0, 0, 0xdeadbeef);
image1x1 = onexoneCanvas.snapshot(null, null);
}
/**
* Convert a AWT image to a JavaFX image.
*
* @param img
* The image to convert.
*
* @return JavaFX image.
*/
public static WritableImage toFXImage(BufferedImage img) {
// This is a stripped down version of "SwingFXUtils.toFXImage(img, fxImg)"
int w = img.getWidth();
int h = img.getHeight();
// Ensure image type is ARGB.
switch(img.getType()) {
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
break;
default:
// Convert to ARGB
BufferedImage converted = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2d = converted.createGraphics();
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
img = converted;
break;
}
// Even if the image type is ARGB_PRE, we use "getIntArgbInstance()"
// Using "getIntArgbPreInstance()" removes transparency.
WritableImage fxImg = new WritableImage(w, h);
PixelWriter pw = fxImg.getPixelWriter();
int[] data = img.getRGB(0, 0, w, h, null, 0, w);
pw.setPixels(0, 0, w, h, PixelFormat.getIntArgbInstance(), data, 0, w);
return fxImg;
}
/**
* Determine the optimal BufferedImage type to use for the specified
* {@code fxFormat} allowing for the specified {@code bimg} to be use as a
* potential default storage space if it is not null and is compatible.
*
* @param fxFormat
* the PixelFormat of the source FX Image
* @param bimg
* an optional existing {@code BufferedImage} to be used for
* storage if it is compatible, or null
* @return
*/
private static int getBestBufferedImageType(PixelFormat<?> fxFormat, BufferedImage bimg) {
if (bimg != null) {
int bimgType = bimg.getType();
if (bimgType == BufferedImage.TYPE_INT_ARGB || bimgType == BufferedImage.TYPE_INT_ARGB_PRE) {
// We will allow the caller to give us a BufferedImage
// that has an alpha channel, but we might not otherwise
// construct one ourselves.
// We will also allow them to choose their own premultiply
// type which may not match the image.
// If left to our own devices we might choose a more specific
// format as indicated by the choices below.
return bimgType;
}
}
switch (fxFormat.getType()) {
default:
case BYTE_BGRA_PRE:
case INT_ARGB_PRE:
return BufferedImage.TYPE_INT_ARGB_PRE;
case BYTE_BGRA:
case INT_ARGB:
return BufferedImage.TYPE_INT_ARGB;
case BYTE_RGB:
return BufferedImage.TYPE_INT_RGB;
case BYTE_INDEXED:
return (fxFormat.isPremultiplied() ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_ARGB);
}
}
public void setPixelFormat(ColourMapEvent event) {
int[] colors = new int[event.getNumberOfColor()];
int r, g, b;
for (int i = event.getFirstColor(); i < colors.length; i++) {
r = event.getColors().readUnsignedShort();
g = event.getColors().readUnsignedShort();
b = event.getColors().readUnsignedShort();
colors[i] = (0xff << 24) | ((r >> 8) << 16) | ((g >> 8) << 8) | (b >> 8);
}
pixelFormat.set(PixelFormat.createByteIndexedInstance(colors));
}
private VLCWindowDirect() {
runOnVLCThread(new Runnable() {
@Override
public void run() {
try {
Utils.fxRunAndWait(new Runnable() {
@Override
public void run() {
stage = new Stage(StageStyle.UNDECORATED);
PlatformUtils.setFullScreenAlwaysOnTop(stage, true);
stage.focusedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue.booleanValue()) {
//focused
stage.toBack();
} else {
//not focused
}
}
});
borderPane = new BorderPane();
borderPane.setStyle("-fx-background-color: black;");
canvas = new Canvas(1920, 1080);
canvas.getGraphicsContext2D().setFill(Color.BLACK);
borderPane.setCenter(canvas);
scene = new Scene(borderPane);
scene.setFill(Color.BLACK);
pixelWriter = canvas.getGraphicsContext2D().getPixelWriter();
pixelFormat = PixelFormat.getByteBgraPreInstance();
}
});
mediaPlayer = new MediaPlayerComponent();
mediaPlayer.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void finished(MediaPlayer mp) {
if (mediaPlayer.getMediaPlayer().subItemCount() > 0) {
String mrl = mediaPlayer.getMediaPlayer().subItems().remove(0);
mediaPlayer.getMediaPlayer().startMedia(mrl);
Platform.runLater(new Runnable() {
@Override
public void run() {
canvas.setVisible(true);
}
});
}
}
});
Utils.fxRunAndWait(new Runnable() {
@Override
public void run() {
stage.setScene(scene);
}
});
init = true;
LOGGER.log(Level.INFO, "Video initialised ok");
} catch (Exception ex) {
LOGGER.log(Level.INFO, "Couldn't initialise video, almost definitely because VLC (or correct version of VLC) was not found.", ex);
}
}
});
ScheduledExecutorService exc = Executors.newSingleThreadScheduledExecutor();
exc.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if (init) {
runOnVLCThread(new Runnable() {
@Override
public void run() {
mediaPlayer.getMediaPlayer().setAdjustVideo(true);
int hueVal = (int) (hue * 360);
if(hueVal>180) hueVal-=360;
hueVal += 180;
mediaPlayer.getMediaPlayer().setHue(hueVal);
Platform.runLater(new Runnable() {
@Override
public void run() {
if (stage != null) {
stage.toBack();
}
}
});
}
});
}
}
}, 0, 30, TimeUnit.MILLISECONDS);
}