Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself
Accepted Solution

display image from network

Hello,

 

How do you load and display an image when it will be read from a socket connection? I can only find samples for loading from file system.

 

I can get the bytes but I cannot find a starting point to display these bytes as a jpeg image using screen library. 

 

Thanks.

 

 

Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself

Re: display image from network

If the data I receive over the network is jpeg image data - would I have to save the image to file to display it or can i somehow display it using screen_blit? (I am new to native programming).

 

I cannot find an answer to this question on the forum or anywhere else.

Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself

Re: display image from network

Hi, sorry to keep bumping this thread, I really somebody help me with this problem I posted days ago and still cant solve myself, I am coming from java to learn native to continue developing for Blackberry. My native skills are limited.

 

What I need to do is read an image from a socket, even this is causing me an issue - I have string references like /dev/shmem/ in my test jpg output file and I also seen "socket" when I opened the file up in a text editor to inspect. I can read in a loop until a certain amount is read but is sprintf(complete_data, read_chars_buf) the right way of appending the data?

 

char buf[1024];
	memset(buf,0,sizeof(buf));
	char complete_data[image_size];
	
	while (totalBytes != image_size)
	{
		received = recv(imagesock, buf, sizeof(buf), 0);
		totalBytes += received;
		sprintf(complete_data, buf);
		

	}

 

 

Once I can correctly read the data I want to convert this to an img_t - I have the following code to do this but I don't know if it is correct because I don't know if I have ever correctly read all the data yet.

 

Would this work if I have read the data correctly?

rc = img_lib_attach(&ilib);
	img_t img;
	memset(&img, 0, sizeof(img));
	img_decode_callouts_t callouts;
	memset(&callouts, 0, sizeof(callouts));
	callouts.setup_f = decode_setup;
	callouts.abort_f = decode_abort;
	callouts.data = (uintptr_t)externalscreen;

	img.flags = 0;
	fprintf(stdout, "imgamount: %d\n",  sizeof(complete_data));
	io_stream_t* memory_stream = io_open(IO_MEM, IO_READ, totalBytes, &complete_data);
	if (( rc = img_load(ilib, memory_stream, &callouts, &img)) != IMG_ERR_OK) {
		fprintf(stderr, "img_load_file() failed: %d\n",  rc);
		//return -1;
	}

 

Please let me know if I need to provide any further information.

Developer
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x

Re: display image from network

First of all I'd advise you to solve the task of reading the image from socket into the file correctly.

You're not handling it correctly now.

Firs of all this

sprintf(complete_data, buf);

is not correct. Don't use sprintf, I think it's more appropriate to memcpy or direcly read into the buffer but to the right position, goggle example. When you read and save file correctly into some 'temp' folder loading/showing the image will not be a problem.

 

 

Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself

Re: display image from network

Thank you for your answer - This is just basic code though, I am only adding the output to file for testing, eventually the images will be displayed directly and will be coming in a continous stream so I want to avoid saving to file and just convert the data to img_t - is my approach correct regarding this where i use io_stream_t do you think?

 

I will google memcpy now.

Developer
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x

Re: display image from network

I don't *think* it is correct. But I might be wrong. Sorry, don't have much experience with this library.

Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself

Re: display image from network

[ Edited ]

I have tried memcpy and my jpg file still ends up with /dev/socket/2 and other weird references to pps and socket in whats meant to be the image data (opened in notepad copied from target file descriptor into filesystem).

 

I now use the following to read the data - the socket should be fine as i use it to read the size of the file beforehand (server prints size and then file). Can anybody see a reason for strange output in file could it be related to endianess as server is java?

 

char buf[1024];
	char complete_data[image_size];
	while (totalBytes != image_size)
	{
		received = recv(imagesock, buf, sizeof(buf), 0);
		totalBytes += received;
		//sprintf(complete_data, buf);
		memcpy(complete_data, buf, sizeof(buf) );


	}
	int rc;
	FILE *fp = fopen("./data/test.jpg","wb"); // open the file
	 
	 rc =   fwrite(complete_data, image_size, 1, fp); // fwrite the data
	 

 

 

 

Developer
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x

Re: display image from network

1. Zero complete_data's memory first, this way you'll make sure there is no garbage.

2. Here

memcpy(complete_data, buf, sizeof(buf) )

you always write data to the beginning of complete_data, so if you have several cycles of reading you just overwrite previous data.

 

Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself

Re: display image from network

thanks, I was already seeing that mistake since last post and have refined the code. if I bzero the arrays, then read data (succesfully in chunks according to my console) and then check the file, the file file just contains lots of nulls. If I don't bzero then the file contains what could be image data but also file paths relvent to playbook.

 

I am very confused about this. My code now looks like..

 

char buf[1024];
	//memset(buf, 0x00, 1024);
	char complete_data[image_size];
	bzero(complete_data, image_size);
	bzero(buf, 1024);
	//memset(complete_data, 0x00, image_size);
	while (totalBytes < image_size)
	{

		received = recv(imagesock, buf, sizeof(buf), 0);
		if(received > 0)
		{
			printf("reading %i \n ", received);
				fflush(stdout);
		totalBytes += received;
		//sprintf(complete_data, buf);
		memcpy(complete_data+received, buf, sizeof(received));
		//memset(buf, 0x00, 1024);
		}


	}

 

Developer
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x

Re: display image from network

Try something like:

char complete_data[image_size];
bzero(complete_data, image_size);
int totalBytes = 0;
while (totalBytes < image_size)
{

	received = read(imagesock, &complete_data[totalBytes], sizeof(complete_data)-totalBytes);
	if(received > 0)
	{
		printf("reading %i \n ", received);
		fflush(stdout);
		totalBytes += received;
	}
}