We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Display blob from MySQL
Page Index Toggle Pages: 1
Display blob from MySQL (Read 12837 times)
Display blob from MySQL
Dec 12th, 2005, 11:59am
 
Hi,
I'm new to Processing and working with the MySQL library. I'm trying to get a photo from a MySQL database to load into my applet, then apply a red tint and transparency once displayed.

Here is a portion of my code:

if (msql.connect () ) {
   msql.query ("SELECT photo FROM database.table");
   
   while (msql.next() ) {
     String blob = (String) msql.getObject("photo");
      }
  }
  else {
  }
  }
void draw () {
   dbphoto = loadImage ("photo");
    tint (200,0,2,126); // Tint red and set transparency
    image (dbphoto,50,50);
}


Any help would be greatly appreciated!
Re: Display blob from MySQL
Reply #1 - Dec 13th, 2005, 5:21pm
 
in what format is the image stored as a blob? can't you get the blob as a byte array?

if so, you'll need to do something like this:

Code:

Image awtImage = Toolkit.getDefaultToolkit().createImage(bytes);

MediaTracker tracker = new MediaTracker(this);
tracker.addImage(awtImage, 0);
try {
tracker.waitForAll();
} catch (InterruptedException e) { }

PImage dbphoto = new PImage(awtImage);


if you can only get a blob as a String, you can try:

byte bytes[] = blob.getBytes();

but that may not work if the image data is somehow misinterpreted as unicode.

also, this should be done inside setup(), or inside mousePressed() or something like that. as noted in the reference, you'll get killed on the speed if you call loadImage() inside draw().
Re: Display blob from MySQL
Reply #2 - Dec 14th, 2005, 8:56pm
 
You can use javax.imageio.ImageIO to read an image from a file, input stream, or URL. javax.imageio is a package added since J2SE 1.4. I have utilized it with the getByteStream from the JDBC Blob object to retrieve an image. This is a sample applet I put together:

import java.awt.Image;
import java.io.BufferedInputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.imageio.ImageIO;

import processing.core.PApplet;
import processing.core.PImage;

@SuppressWarnings("serial")
public class ImageViewer extends PApplet {
   PImage pic = null;

   public void setup() {
       Connection conn = null;
       ResultSet rs = null;
       BufferedInputStream is = null;

       try {
           // create connection and retrieve the blob
           Class.forName("org.gjt.mm.mysql.Driver");
           String url = "jdbc:mysql://localhost:3306/blobtest";
           String user = "root";
           String password = "password";

           conn = DriverManager.getConnection(url, user, password);
           Statement stmt = conn.createStatement();
           rs = stmt
                   .executeQuery("select photo from photoTable where id = 'Something'");

           if (rs.next()) {
               // retrieve tbe image using ImageIO
               Blob blob = rs.getBlob("photo");
               is = new BufferedInputStream(blob.getBinaryStream());

               Image raw = ImageIO.read(is);
               pic = new PImage(raw);
           }
       } catch (Exception e) {
       } finally {
           try {
               is.close();
           } catch (Exception e) {
               // add exception handling
               e.printStackTrace();
           }
           try {
               rs.close();
               conn.close();
           } catch (Exception e) {
               // add exception handling
               e.printStackTrace();
           }

       }
   }

   public void draw() {  
       // display the image
       if (pic != null) {
           image(pic, 0, 0);
       }
       noLoop();
   }
}
Re: Display blob from MySQL
Reply #3 - Feb 23rd, 2007, 3:36am
 
pablocomotion wrote on Dec 12th, 2005, 11:59am:
...

   while (msql.next() ) {
     String blob = (String) msql.getObject("photo");
      }

...

void draw () {
   dbphoto = loadImage ("photo");
    tint (200,0,2,126); // Tint red and set transparency
    image (dbphoto,50,50);
}


doesn't loadImage load an image from a filename and regardless of that, what does "photo" refer to, other than the column identifier in a row from the mysql table the variable you created before to store the variable was called blob.. also, i believe its scope may be only within the while loop inside which it was created.

but i dunno, i'm a c/c++ programmer who stumbled onto processing and fell in love with it. i'm still new.
Re: Display blob from MySQL
Reply #4 - Feb 23rd, 2007, 11:46am
 
btw, i got the JDBC code to run and it works fine.. but i think it's having issues displaying the awt-generated images from within Processing, and you may be forced to embed your Processing sketch.
Re: Display blob from MySQL
Reply #5 - Feb 23rd, 2007, 1:21pm
 
if you are using my mysql library, then you can do:

java.sql.Blob blob = mysql.result.getBlob("imgTable");
byte[] imgBytes = blob.getBytes(0,blob.length());

or

ImputStream in = blob.getBinaryStream();

F
Re: Display blob from MySQL
Reply #6 - Feb 23rd, 2007, 1:38pm
 
sweet. this way, i may be able to eliminate the awt image functions altogether..

is "bytes" a data type, or should i create an array of bytes with "byte[]" ? and if "bytes" is its own data type, is this the sort of object that needs to be passed to the image creation function?
Re: Display blob from MySQL
Reply #7 - Feb 23rd, 2007, 2:53pm
 
byte[]

sorry.

F
Re: Display blob from MySQL
Reply #8 - Feb 24th, 2007, 12:38am
 
by the way, neither method worked natively in Processing, but i mixed the two methods (using the mysql wrapper library for db, but ImageIO for binary stream) and got it to compile in Eclipse. i had to do a bit of JAR tweaking, but it worked.

i'll get to writing my walkthrough for adding your applets to java programs.. but for now, here is the java source code.

MainClass.java
Code:
package curseMap;

import de.bezier.mysql.*;

import java.awt.*;
import java.io.*;
import java.util.*;

import javax.imageio.ImageIO;

import processing.core.*;


// applet class
//
public class MainClass extends PApplet {

private static final long serialVersionUID = 1L;
ArrayList tiles = new ArrayList();
int[][] map = new int[9][9];
int x, y;
MySQL msql;

// initializing function
//
public void setup()
{
 grabTiles();
 x = 4;
 y = 4;
 grabMap("newbie_zone");
 size(288, 288);
 background(0);
}

// main applet function
//
public void draw()
{
 for(int a = 0; a < 9; a++){
   for(int b = 0; b < 9; b++){
     image((PImage) tiles.get(map[a][b]), b*32, a*32);
   }
 }
     
 noLoop();
}
   
// populate tiles list
//
public void grabTiles()
{
   String db = "db";
   String user = "user";
   String password = "password";
   int a = 0;
   
   msql = new MySQL("some.server.com", db, user, password, this);
   
   if(msql.connect()){    
     msql.query("select blob_content, blob_id from tiles__map order by blob_id asc");
   
   while (msql.next()) {
     try {
       java.sql.Blob blob = msql.result.getBlob("blob_content");
       InputStream is = blob.getBinaryStream();
       Image raw = ImageIO.read(is);
       PImage norm = new PImage(raw);
       tiles.add(a++, norm);
   } catch (Exception e) {
       e.printStackTrace();
   }
  }
 }
}

// populate map cells
//
public void grabMap(String mapName)
{
 String tbl = "map__" + mapName;
 String db = "db";
 String user = "user";
 String password = "password";

 msql = new MySQL("some.host.com", db, user, password, this);
 
 if(msql.connect()){  
   msql.query("select x, y, tile from " + tbl + " where "
     + "x >= " + (x - 4) + " AND "
     + "x <= " + (x + 4) + " AND "
     + "y >= " + (y - 4) + " AND "
     + "y <= " + (y + 4)
     + " order by x asc, y asc");    

 msql.next();

 for(int a = 0; a < 9; a++)
   for(int b = 0; b < 9; b++)
     if(msql.getInt("x") != a || msql.getInt("y") != b){
       map[b][a] = 0;
     } else {
       map[b][a] = msql.getInt("tile");
       msql.next();
     }
 }
}

static public void main(String args[]) {
 PApplet.main(new String[] { "curseMap.MainClass" });
}

}


ExampleFrame.java
Code:
package curseMap;

import java.awt.BorderLayout;
import java.awt.Frame;

import processing.core.*;

public class ExampleFrame extends Frame {

 private static final long serialVersionUID = 1L;

 public ExampleFrame() {
   super("Embedded PApplet");
   setLayout(new BorderLayout());
   PApplet embed = new curseMap.MainClass();
   add(embed, BorderLayout.CENTER);

   // important to call this whenever embedding a PApplet.
   // It ensures that the animation thread is started and
   // that other internal variables are properly set.
   embed.init();
 }
}
Re: Display blob from MySQL
Reply #9 - Feb 14th, 2009, 4:38am
 
how do i get this to run in processing?
Page Index Toggle Pages: 1