PixelWriten画布不会显示在屏幕上

时间:2019-07-02 19:28:37

标签: javafx canvas pixel tornadofx

我有一个与像素RGB值相对应的int数组,并试图将它们写入图像,然后将其添加到视图中显示的画布上。 我已经遵循JavaFX docs指南来使用pixelWriter,而且我知道将画布添加到视图的语法是正确的,因为我在我的应用程序的其他地方也做了相同的操作,并测试了将正确的画布正确放置在这里。

我尝试了上下文方式: 查看课程:

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几乎在每次调用时都在写入位,但是递增值的速度缓慢(趋势是这样的: write val pos = 12,30,62,12,30,62,78,99,10 ... 如果怀疑是问题,我可以抄下确切的数字)

在这两种情况下,我都认为问题在于填充画布后对其进行更新,但是我仅通过在模型中设置标记后才添加画布来消除这种理论-除画布标题外,什么都没有出现。 / p>

我花了整整一个工作日来尝试显示此消息,对您的帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

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