gpt4 book ai didi

java - 在java中将图像的像素复制到 Canvas

转载 作者:行者123 更新时间:2023-11-30 07:00:45 24 4
gpt4 key购买 nike

编辑:我更改了一些代码以反射(reflect) AlamasB 所说的内容。我仍然不明白应该如何填充字节数组以及需要进行哪些转换。

我正在尝试操作用户上传的图像的像素并将新图像写入 Canvas 上。现在我只是尝试复制原始图像而不更改像素的 RGB。我已经被困在同一个地方很长一段时间了,不知道下一步该去哪里。在代码的第一部分中,我复制了用户上传的原始图像。

   Image image = imageView.getImage();
PixelReader pixelReader = image.getPixelReader();
PixelFormat format = pixelReader.getPixelFormat();

int width= (int)image.getWidth();
int height = (int) image.getHeight();

GraphicsContext gc = canvas.getGraphicsContext2D();
PixelWriter pw = gc.getPixelWriter();

byte[] imageData = new byte[width * height * 4];
imageData = createImageData(imageData, pixelReader, width, height);

//..
//...the next function populates my byte array with what's in the image

public byte[] createImageData(byte[] imageData, PixelReader pr, int width, int height){
int i = 0;
for(int y=0; y<height; y++){
for(int x = 0; x < width; x++){
int argb = pixelReader.getArgb(x, y);
imageData[i] = (byte) argb;
imageData[i+1] = (byte) argb;
imageData[i+2] = (byte) argb;
i+=3;
pw.setArgb(x, y, argb);
}
}


return imageData;
}

编辑:不再使用此功能。下一个功能真的让我很困惑。我指的是这个http://docs.oracle.com/javafx/2/image_ops/jfxpub-image_ops.htm作为引用,但我不知道发生了什么。

//...  
//... the next function sets the pixels of the canvas to what's in the byte array

public void drawImageData(byte[] imageData, PixelWriter pw, int width, int height){
boolean on = true;
PixelFormat<ByteBuffer> pixelFormat = PixelFormat.getByteRgbInstance();
for(int y = 50; y < 150; y+=height){
for(int x = 50; x < 150; x+=width){
if(on){
pw.setPixels(x, y, width, height, pixelFormat, imageData, 0, width*3);
}
on = !on;
}
on=!on;
}
}

最佳答案

考虑到 PixelReader 提供 getARGB在这种情况下,最好的选择可能是使用 32 位表示。随后将其转换为 4 字节数组,即 width * height * 4。最后使用setARGB绘制图像。

int argb = pixelReader.getArgb(x, y);
// populate imageData[i,i+1,i+2,i+3] by converting argb
...
// convert imageData[i,i+1,i+2,i+3] into a 32bit argb, say int argb2
pixelWriter.setArgb(x, y, argb2);

或者,您可以使用 Color 的 getOpacity()RGBA 获取 A。再次使用 32 位表示。

至少这将为您提供一个最小的工作示例,然后您可以扩展它。

这是一个快速版本(独立,只需复制和粘贴):

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ImageManipulationApp extends Application {

private static final int APP_W = 800;
private static final int APP_H = 600;

private Parent createContent() {
Image image = makeMockImage();
byte[] imageData = imageToData(image);

byte[] modifiedImageData = modify(imageData);
Image modifiedImage = dataToImage(modifiedImageData);

HBox root = new HBox(25);
root.getChildren().addAll(new ImageView(image), new ImageView(modifiedImage));

return root;
}

private Image makeMockImage() {
WritableImage image = new WritableImage(APP_W / 2, APP_H);
PixelWriter writer = image.getPixelWriter();

for (int y = 0; y < APP_H; y++) {
for (int x = 0; x < APP_W / 2; x++) {
writer.setColor(x, y, Math.random() < 0.00005 ? Color.YELLOW : Color.BLACK);
}
}

return image;
}

/**
* Modifies the pixel data.
*
* @param data original image data
* @return modified image data
*/
private byte[] modify(byte[] data) {
// this is where changes happen
return data;
}

private byte[] imageToData(Image image) {
int width = (int) image.getWidth();
int height = (int) image.getHeight();

byte[] data = new byte[width * height * 4];

int i = 0;

for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
int argb = image.getPixelReader().getArgb(x, y);

byte[] pixelData = ByteBuffer.allocate(4).putInt(argb).array();

data[i++] = pixelData[0];
data[i++] = pixelData[1];
data[i++] = pixelData[2];
data[i++] = pixelData[3];
}
}

return data;
}

private Image dataToImage(byte[] data) {
// if we don't know the image size beforehand we can encode width and height
// into image data too
WritableImage image = new WritableImage(APP_W / 2, APP_H);
PixelWriter writer = image.getPixelWriter();

int i = 0;
for (int y = 0; y < APP_H; y++) {
for (int x = 0; x < APP_W / 2; x++) {
int argb = ByteBuffer.wrap(Arrays.copyOfRange(data, i, i + 4)).getInt();

writer.setArgb(x, y, argb);

i += 4;
}
}

return image;
}

@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

关于java - 在java中将图像的像素复制到 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40966172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com