温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javafx - PixelWriten canvas won't display on screen
canvas javafx pixel tornadofx

javafx - PixelWriten画布不会显示在屏幕上

发布于 2020-03-27 11:56:17

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

我尝试了上下文方式:View class:

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文章中提到的WritableImage方法,该方法是从int数组创建Javafx图像的

查看类别:

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)
     }
}

function to get IR RGBInt from float value of temperature (shown to be correct on RasPi that acquires data, and I have compared my received data vs snet RasPi data)

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

For the first method, there are 133K dirty bits written in the canvas and when I use the debugger it seems that PixelWriter.setPixels is writing over bits on almost every call but slowly progressing in increasing values (the trend is like this: write val pos = 12, 30, 62, 12, 30, 62, 78, 99, 10 ... I can copy down the exact numbers if this is suspected to be the issue)

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

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

查看更多

查看更多

提问者
Frankie Guzikowski
被浏览
61
Frankie Guzikowski 2019-07-03 23:28

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