Receiving JPEG packet from camera


 
Thread Tools Search this Thread
Top Forums Programming Receiving JPEG packet from camera
# 1  
Old 12-16-2010
Receiving JPEG packet from camera

I am trying to receive a packet of data as bytes in C, but the picture is getting messed up.

I am using fwrite to append bytes to jpg file, but the append or write does not
seem to be appending jpg correctly.

Packet 1 data comes in append to file
Packet 2 data comes in append to file
...
N packets

Code:
void BWrite(unsigned char *b){
    FILE *fp;
    fp = fopen(filename, "ab");
    fwrite(b,1,1,fp);
    fclose(fp);
}

The computer runs centos on flash hard drive.

My brain is fried and out of ideas on how to get this JPEG looking right instead of as bad looking jigsaw puzzle Smilie
# 2  
Old 12-16-2010
You're not using the file I/O buffering that fopen gives you, so you may as well use open, which will be quicker...try:
Code:
void BWrite(unsigned char *b){
    int fd;
    fd=open(filename,O_CREAT|O_APPEND,0700);
    write(fd,b,1);
    close(fd);
}

# 3  
Old 12-16-2010
Thanks for reply.

OK, that created the file, but left file empty with 0 bytes.

Any other ideas, my brain hurts.Smilie

Also, JPEG is Big-Endian and my CentOS is Little-Endian, so I was trying to
see if reversing the char would correct, but have been unsuccessful so far.
Not sure if this is correct path to fix problem or if writing to disk is problem.

Open to any suggestions, thanks.

---------- Post updated at 11:09 AM ---------- Previous update was at 10:49 AM ----------

Changing your code to:

Code:
void BWrite(unsigned char *b){
    FILE *fd;
    fd=fopen(filename,"ab+");
    write(fd,b,1);
    fclose(fd);
}

wrote to file, but it is not opening as jpg file still???

Last edited by photon; 12-20-2010 at 02:26 PM.. Reason: correct code
# 4  
Old 12-17-2010
You can't mix fopen and write() calls. fopen belongs with fwrite, open belongs with write.

Your first code wasn't wrong, either. It looks like it should write one more byte onto the end of the file.

I think what's wrong is the fact that you're writing single bytes. Are you sure your packets are single bytes? That seems really odd! You might be throwing out 99% of the packet.
# 5  
Old 12-20-2010
I am getting all the data as bytes in packets.

What ways are there to wright data blocks of bytes?

Maybe 10 bytes a block.

Would this affect the JPEG image size, because all jpg files
have the same size, even when camera sends different size
images.
# 6  
Old 12-20-2010
Quote:
Originally Posted by photon
I am getting all the data as bytes in packets.
Of course you are. Anything chunk of data whatsoever can be represented as 8-bit bytes. But that doesn't mean you're getting only one byte!
Quote:
What ways are there to wright data blocks of bytes?
You already know 2 different ways. The difference is that, instead of using them to write 1 byte, you use them to write more.
Quote:
Maybe 10 bytes a block.
Do you actually know that? Do you know your packets are even all the same size? You shouldn't have to guess -- something, somewhere in your code must know the size of the packet you received, but you're either not keeping that result or not using it...

Unfortunately we can't see your code from here. Please post it.

Quote:
Would this affect the JPEG image size, because all jpg files
have the same size, even when camera sends different size
images.
I certainly hope it would affect the jpeg image size, right now they're only going to be a tiny fraction of the size they should be.
# 7  
Old 12-20-2010
I don't have documentation that is the problem and I am forced to try and understand how to get this JPEG image correct.

Documentation does say image size is

Code:
0xNNMMZZYY - Image Size 32 bits

The data for example gives me:
NN=0
MM=0
ZZ=10
YY=64

I know how many data bytes total from the formula I calulated.
((zz - 1) * 259) + 256 + YY = DataSize for individual packets

Don't know what the formula means, but it works, so I tried to append
the bytes one by one, but got a fuzzy image and I know the camera works
because the older pascal code works and uses a Blockwrite function:
Code:
 Blockwrite(MyFileHandle, byte, 1, BytesRead);

which writes byte by byte in a loop, similar to the way I first explained and tried to emulate.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Packet loss coming with big packet size ping

(5 Replies)
Discussion started by: Vishal_dba
5 Replies

2. Shell Programming and Scripting

renaming jpeg files

Hi guys im currently trying to write a script which includes rename files and this is the part where i fail i have a list of files eg and i want to rename them so any help really would be appreciated Cheers dunryc (7 Replies)
Discussion started by: dunryc
7 Replies

3. Programming

Receiving broadcast packets using packet socket

Hello I try to send DHCP RENEW packets to the network and receive the responses. I broadcast the packet and I can see that it's successfully sent using Wireshark. But I have difficulties receiving the responses.I use packet sockets to catch the packets. I can see that there are responses to my... (0 Replies)
Discussion started by: xyzt
0 Replies

4. Shell Programming and Scripting

makefile to convert .jpeg to .png thumnails

Hello! I'm desperately trying to write a makefile that converts and scales jpeg files to png files in subdirectories I want to use netpbm, so I just need the commands: anytopnm pnmscale pnmtopng For various reasons i want to use a makefile. This is the first time i use the make tool... (1 Reply)
Discussion started by: henningbaer
1 Replies

5. Programming

How to insert JPEG on each side of the cube using openGL in C

Hi, how to insert JPEG on each side of the cube using OpenGL in C language.. i have a program for cube.. guide me ,,, any answer will valuable.. (3 Replies)
Discussion started by: Ravikishore
3 Replies

6. UNIX Desktop Questions & Answers

problem with camera ID 0c45:613b Microdia Win2 PC Camera Ubuntu

Hi to all Like in the subject, i got the problem with camera ID 0c45:613b Microdia Win2 PC Camera under Ubuntu. When i plug in camera to USB, Ubuntu put in sn9c102 module, but on this site Linux Kernel Driver Database: CONFIG_USB_GSPCA_SONIXJ: SONIX JPEG USB Camera Driver is information that... (0 Replies)
Discussion started by: m0z4rt
0 Replies

7. IP Networking

Problem Receiving the first OSPF packet

I trying to send and receive OSPF packets. I am using RAW Sockets(socket(AF_INET, SOCK_RAW, IPPROTO_OSPF)) to do this. I am successfully able to send an OSPF Hello packet however I am not able to receive a OSPF packet if I have not sent an OSPF packet earlier on the RAW SOCKET. Scenario: ... (3 Replies)
Discussion started by: cosmic_egg
3 Replies

8. UNIX for Dummies Questions & Answers

how do I view a jpeg?

I use gv or ggv to view a .pdf, or .ps file. What will allow me to see a jpeg file? If it matters, I'm using xterm on a PowerBook G4. (0 Replies)
Discussion started by: dsstamps
0 Replies

9. UNIX for Advanced & Expert Users

Jpeg viewer on SunOS 5.8

I was wondering what program can i use instead of mozilla to view a picture on a sun solaris system running SunOS 5.8. To view a pic i'm currently doing it this way: xterm -sb -fn screen-bold -g 60x30+0-0 -title '%s window - pls quit window upon completion ' -e mozilla file:///usr/sailing.JPG ... (4 Replies)
Discussion started by: wisher115
4 Replies

10. UNIX for Dummies Questions & Answers

Convert Jpeg to useable Unix Pics

I'm running a Sunblade 1500 with 2 XVR-100 video cards. I have Xinerama runing as well. Desktop is CDE. Can someone help me in converting Jpeg pictures so that I can use them as backdrops? Is there a way to make the picture come on both monitors instead of streaching it across both? Thanks. (1 Reply)
Discussion started by: HSTheDuck
1 Replies
Login or Register to Ask a Question