简体   繁体   中英

Filtering postgres data using bytea column

I have a java application that inserts stock items as they are received from the supplier. I capture the bar-code image of the the product along with other information(name,price & so on..). I'm saving the bar-code image inform of bytea in PostgreSQL database. Since I do not yet have a bar-code scanner, I'm taking a photo of the bar-code image and inserting as follows.

  //code extract
    File imgFile = new File(barcodeImage.jpg);
    FileInputStream fin=new FileInputStream(imgFile);
    pstmt2.setBinaryStream(19, fin,  (int) imgFile.length());  //inserts      into barcodeImage - bytea column

I now want to retrive the product from the database using the barcode image i had saved. something like :

         select * from stock_item where barcodeImage=***

How do I go about to filter the data using the barcodeImage(bytea) column? Just like how the barcode scanners work.

The only way is to scan the bar-code image and store the code as varchar. IMHO

If you wanted to do this in the database, you'd need a library that could decode barcodes from raster images (PNG, JPEG, etc), and you'd need to expose that as a PostgreSQL function via a procedural language (PL/Python, PL/Perl, C, etc).

Personally I recommend not doing this in the DB. Hand the work out to a pool of external helper processes, each of which analyses a block of codes and updates the DB with the code (if any) found in the image. This gives you more choice of language, isolates the database from any crashes in the barcode decoding software, etc.

My first search found zxing , a Java library for barcode decoding. So I'd probably use that with PgJDBC to produce a simple worker process that fetched a barcode image, decoded it, and updated the row with the decoded barcode. If for some reason I really had to do this in the DB I'd either use PL/Java (warning, here be dragons) or find a barcode decoding library for a better supported in-database language.

Trying to do it like you describe, in the WHERE clause, will be utterly hopeless. You'd have to decode the barcode for every image in every query. Completely unmanagable. A functional index would work, but would be hugely expensive to create and a nightmare to maintain in the face of updates to the barcode lib, etc. You need to preprocess this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM