edgarbarrero
edgarbarrero

Reputation: 51

How to convert a byte array to a png image

I want to convert a byte string from Ice server to a png as frequent as 30 times per second. I use chunky_png gem with this code:

data = @@cprx.getImageData()
width= data.description.width   
height = data.description.height
png = ChunkyPNG::Image.new(width,height, ChunkyPNG::Color::TRANSPARENT)
pixeles = data.pixelData.bytes.to_a
k=0
for i in 0..height-1
  for j in 0..width-1
    png[j,i]=ChunkyPNG::Color.rgb(pixeles[k],pixeles[k+1],pixeles[k+2])
    k=k+3
  end
end
image = png.to_data_url

I create an image and I give values pixel by pixel. But it is too slow. I would like to know if there is a faster method.

Upvotes: 0

Views: 859

Answers (1)

edgarbarrero
edgarbarrero

Reputation: 51

Finally I have solved the problem with the method from_rgb_stream. Besides there is a library equivalent to chunky_png called oily_png that is faster thanks to a c++ core. My code currently looks like:

def ice_camera
    status = 0
    ic = nil
    if session[:conected2] == nil
        arguments= ["--Ice.Config=cameraview.cfg"]
        ic = Ice::initialize(arguments)
        base = ic.propertyToProxy("Cameraview.Camera.Proxy")
        cprx = Jderobot::CameraPrx::checkedCast(base)
        session[:conected2] = true
        thr1 = Thread.new do
            @@mutex = Mutex.new
            while true do
                puts "11111111111111111111111111111111111111111"
                data = cprx.getImageData()
                png = ChunkyPNG::Canvas.from_rgb_stream(data.description.width, data.description.height, data.pixelData)
                @@mutex.synchronize do
                    @@image=png.to_data_url
                end
            end
        end
    end
end

Upvotes: 2

Related Questions