【问题标题】:PixelWriten canvas won't display on screenPixelWriten 画布不会显示在屏幕上
【发布时间】:2019-11-13 11:36:37
【问题描述】:

我有一个对应于像素 RGB 值的 intArray,我正在尝试将它们写入图像,然后将其添加到视图中显示的画布中。 我遵循了使用 pixelWriter 的 JavaFX 文档指南,并且我知道将画布添加到视图的语法是正确的,因为我在我的应用程序的其他地方做了同样的事情,并测试了在此处正确放置不同的画布。

我已经尝试过上下文方式: 查看类:

vbox(20.0, Pos.BOTTOM_CENTER) {
    if (controller.t > 100) { //same results with or without this
        label("IR camera view title")
        children.add(controller.rPiSensors.canvasIR)
    }
}

模型类:

val canvasIR = Canvas(lIR_WIDTH_RES.toDouble(), lIR_HEIGHT_RES.toDouble())
val gc: GraphicsContext = canvasIR.graphicsContext2D
var lIRPixelWriter = gc.pixelWriter


for (y in 0 until  32) {
    for (x in 0 until 24) {
        lIRPixelWriter.setPixels(0,0,lIR_WIDTH_RES,lIR_HEIGHT_RES, PixelFormat.getIntArgbInstance(), lIRHeatMapPixelInts, 0 , lIR_WIDTH_RES)
     }
}

我也尝试过这篇 SO 帖子Creating a Javafx image from a int array 中提到的 WritableImage 方式:

查看类:

vbox(20.0, Pos.BOTTOM_CENTER) {
    if (controller.t > 100) { //same results with or without this
        label("IR camera view title")
        //tested this too controller.rPiSensors.imageView.show()
        children.add(controller.rPiSensors.imageView)
    }
}

模型类:

var lIRHeatMapPixelInts = IntArray(768) //24*32
//write to this in calculateIRPixelHelper below

var lIRHeatImage = WritableImage(lIR_WIDTH_RES, lIR_HEIGHT_RES)
var lIRPixelWriter = lIRHeatImage.pixelWriter
val imageView = ImageView(lIRHeatImage)

for (y in 0 until  32) {
    for (x in 0 until 24) {
        lIRPixelWriter.setPixels(0,0,lIR_WIDTH_RES,lIR_HEIGHT_RES, PixelFormat.getIntArgbInstance(), lIRHeatMapPixelInts, 0 , lIR_WIDTH_RES)
     }
}

从温度的浮点值获取 IR RGBInt 的函数(在获取数据的 RasPi 上显示是正确的,并且我已经将接收到的数据与 snet RasPi 数据进行了比较)

fun calculateIRPixelHelper(x : Int, y : Int, v : Float) {
    //float color[NUM_COLORS][3] = { {0;0;0}; {0;0;1}; {0;1;0}; {1,1,0},{1,0,0}, {1,0,1}, {1,1,1} }
    val color : Array<FloatArray> = arrayOf(floatArrayOf(0.0f,0.0f,0.0f), floatArrayOf(0.0f,0.0f,1.0f), floatArrayOf(0.0f,1.0f,0.0f), floatArrayOf(1.0f,1.0f,0.0f), floatArrayOf(1.0f,0.0f,0.0f), floatArrayOf(1.0f,0.0f,1.0f), floatArrayOf(1.0f,1.0f,1.0f))
    var outputV = v
    val idx1 : Int
    val idx2 : Int
    var fractBetween = 0.0f
    val vmin = 5.0f
    val vmax = 50.0f
    val vrange = vmax-vmin
    outputV -= vmin
    outputV /= vrange
    val ir : Int
    val ig : Int
    val ib : Int
    val pixelRGBInt : Int
    if (outputV <= 0) {
        idx1= 0
        idx2= 0
    } else if (outputV >= 1) {
        idx1 = (NUM_COLORS-1)
        idx2 = (NUM_COLORS-1)
    } else {
        outputV *= (NUM_COLORS-1)
        idx1 = truncate(outputV).toInt()
        idx2 = idx1+1
        fractBetween = outputV - idx1
    }

    ir = ((((color[idx2][0] - color[idx1][0]) * fractBetween) + color[idx1][0]) * 255.0).toInt()
    ig = ((((color[idx2][1] - color[idx1][1]) * fractBetween) + color[idx1][1]) * 255.0).toInt()
    ib = ((((color[idx2][2] - color[idx1][2]) * fractBetween) + color[idx1] [2]) * 255.0).toInt()
    pixelRGBInt = (ir shl 16) or (ig shl 8) or ib


    for(py  in 0 until IMAGE_SCALE) {
        for(px in 0 until IMAGE_SCALE) {
            lIRHeatMapPixelInts[x + px + IMAGE_SCALE*(y + py)] = pixelRGBInt
        }
    }
}

Here are the float values of the temperature

the pixel ints, which are all 0 beyond the first ~50

And part of the list of values saved in the canvas

对于第一种方法,画布中写入了 133K 脏位,当我使用调试器时,似乎 PixelWriter.setPixels 几乎在每次调用时都在写入位,但值逐渐增加(趋势是这样的: 写 val pos = 12, 30, 62, 12, 30, 62, 78, 99, 10 ... 如果怀疑是这个问题,我可以抄下确切的数字)

在这两种情况下,我都认为问题在于一旦画布被填充就更新画布,但我已经消除了这个理论,只在模型中设置了标志后才添加画布——除了画布的标题之外,仍然没有出现任何内容。

我花了大约一个工作日来尝试显示这个,任何帮助将不胜感激:)

【问题讨论】:

    标签: javafx canvas pixel tornadofx


    【解决方案1】:

    Alpha 位被设置为零,对于非透明像素,必须将它们在 pixelRGBint 的高位设置为非零!

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2021-02-12
      • 2018-10-06
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      相关资源
      最近更新 更多