Hi everyone,
I'm trying to use this code from Daniel Shiffman, translating the necessary parts to Android's graphics library (since Android doesn't include awt libraries).
http://shiffman.net/2010/11/13/streaming-video-with-udp-in-processing/ I'm basically trying to stream video from an Android tablet to a Processing sketch running on a PC, both connected to the same WiFi network. Like using the tablet as a wireless webcam. NOTE: I'm using a still to test this out but I'll use the tablet's camera, that's why I can't use "file to byte array" methods.
I have to convert a PImage to a byte array that is usable with Android's BitmapFactory.decodeByteArray() . How do I do that?
I tried looping through the pixels[] and passing it to an int array but, when I translate this to bytes, the BitmapFactory can't use it.
BitmapFactory docs:
http://developer.android.com/reference/android/graphics/BitmapFactory.html
Here is the image I'm using:
http://3.bp.blogspot.com/-GMpXUe3kKkU/Tgn5CInbVxI/AAAAAAAAIpQ/cs0AICtd9Ao/s320/picasso_woman_b.jpg
Thanks in advance for all the help you can give me!!
Here is the code so far (Line 61 is how I know my byte array is not working, it returns null):
- import java.net.*;
- import java.io.*;
- import java.nio.*;
- import ketai.net.*;
- import android.graphics.*;
- PImage myimg;
- int[] myPixels;
- int clientPort = 9100;
- DatagramSocket ds;
- String myIPAddress;
- String remoteAddress = "192.168.0.104";//PC Local IP address. REPLACE WITH YOUR IP ADDRESS
- void setup()
- {
- size(192,262);
- orientation(PORTRAIT);
- imageMode(CENTER);
- myIPAddress = KetaiNet.getIP();
- myimg = loadImage("picasso_woman_b.jpg");
- try
- {
- ds = new DatagramSocket();
- }
- catch (SocketException e)
- {
- e.printStackTrace();
- }
- broadcast(myimg);
- }
- void draw()
- {
- image(myimg,width/2 , height/2);
- }
- void broadcast(PImage img)
- {
- //Convertir pixels[] en int[]
- myPixels = new int[img.width*img.height];
- img.loadPixels();
- for(int i = 0; i<img.width*img.height; i++)
- {
- myPixels[i]= img.pixels[i];
- }
- //Convert int[] to byte[]
- ByteBuffer byteBuffer = ByteBuffer.allocate(img.width*img.height * 4);
- IntBuffer intBuffer = byteBuffer.asIntBuffer();
- intBuffer.put(myPixels);
- byte[] imgByteArray = byteBuffer.array();
- //Use byte[] with BitmapFactory
- Bitmap bitmap = BitmapFactory.decodeByteArray(imgByteArray, 0, imgByteArray.length);
- println(bitmap);//HERE IS WHERE I FOUND MY BYTE ARRAY IS NOT VALID, SINCE IT RETUNRS NULL
- // Need these output streams to get image as bytes for UDP communication
- ByteArrayOutputStream baStream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baStream);//Compress bitmap to be able to stream it
- BufferedOutputStream bos = new BufferedOutputStream(baStream);
- // Get the byte array, which we will send out via UDP!
- byte[] packet = baStream.toByteArray();
- // Send JPEG data as a datagram
- println("Sending datagram with " + packet.length + " bytes");
- try
- {
- ds.send(new DatagramPacket(packet,packet.length, InetAddress.getByName(remoteAddress),clientPort));
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
1