如何使用 C 编程创建图像数据库
How to create database of images using C programming?
我需要使用C编程创建一个简单的数据库来存储图像。然后我如何使用数据库将图像一个接一个地提供给另一个应用程序。每个映像都有一个 ID。如果任何图像与我的应用程序需求匹配。其相应的 ID 必须打印为输出。
我需要为大约 350 张图像创建数据库。我的主要应用程序是用 C 编写的。那么任何人都可以帮助我如何创建数据库以及如何将其与主应用程序链接吗?
最简单的方法是在sqlite db中存储为BLOB,该数据库在项目中集成为单个.c文件。请参阅示例 C 文件,该文件演示了访问 Blob 的两种不同方法。一个使用绑定,一个使用 blob API。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/time.h>
#include <unistd.h>
#include <libgen.h>
#include "sqlite3.h"
sqlite3 *db;
int error(char *msg) {
fprintf(stderr, "ERROR: %sn", msg);
if(db)
sqlite3_close(db);
exit(1);
}
int gettype(char **imgtype, int columns, char **colval, char **colname) {
int col;
if(columns!=1)
return 1;
snprintf((char *)imgtype, 4, "%s", colval[0]);
return 0;
}
int getrowid(sqlite3_int64 *rowid, int columns, char **colval, char **colname) {
int col;
if(columns!=1)
return 1;
*rowid=(int)strtol(colval[0], NULL, 10);
return 0;
}
int main(int argc, char **argv) {
enum { PROG, FUNC, TABLE, NAME };
sqlite3_blob *dblob;
char sqlcmd[1024];
sqlite3_int64 rowid=0;
sqlite3_stmt *stmt;
char *zErrMsg=0;
char filename[1024];
char outname[1024];
char imgtype[4];
FILE *fblob;
char *blobmem;
long blobsize;
if(argc!=4)
error("usage: blob <get|put> <table> <filename>");
if(sqlite3_open(DATABASE, &db))
error((char*)sqlite3_errmsg(db));
sqlite3_busy_timeout(db, TIMEOUT);
// using bind api
if(strcmp(argv[FUNC], "put")==0) {
snprintf(filename, 1024, "%s", basename(argv[NAME]));
strtok(filename, ".");
snprintf(imgtype, 4, "%s", strtok(NULL, "."));
if(!strlen(imgtype) || !(strcasecmp(imgtype, "gif")==0 || strcasecmp(imgtype, "jpg")==0 || strcasecmp(imgtype, "png")==0))
error("wrong extension / img type");
fblob=fopen(argv[NAME], "r");
if(fblob==NULL)
error("file not found");
fseek(fblob, 0L, SEEK_END);
blobsize=ftell(fblob);
if(!blobsize)
error("wrong file size");
blobmem=malloc(blobsize);
if(!blobmem)
error("unable to allocate memory");
rewind(fblob);
if(fread(blobmem, blobsize, 1, fblob)!=1)
error("unable to read file");
fclose(fblob);
snprintf(sqlcmd, 1024, "delete from '%s' where name like '%s';", argv[TABLE], filename);
if(sqlite3_exec(db, sqlcmd, NULL, NULL, &zErrMsg)!=SQLITE_OK)
error(zErrMsg);
snprintf(sqlcmd, 1024, "insert into '%s'(name, type, data) values(?, ?, ?);", argv[TABLE]);
if(sqlite3_prepare_v2(db, sqlcmd, -1, &stmt, 0)!=SQLITE_OK)
error((char*)sqlite3_errmsg(db));
sqlite3_bind_text(stmt, 1, filename, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, imgtype, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 3, blobmem, blobsize, SQLITE_STATIC);
if(sqlite3_step(stmt)!=SQLITE_DONE)
error((char*)sqlite3_errmsg(db));
if(sqlite3_finalize(stmt)!=SQLITE_OK)
error((char*)sqlite3_errmsg(db));
free(blobmem);
}
// using blob api
else if(strcmp(argv[FUNC], "get")==0) {
snprintf(filename, 1024, "%s", argv[NAME]);
strtok(filename, ".");
snprintf(sqlcmd, 1024, "select rowid from '%s' where name like '%s';", argv[TABLE], filename);
if(sqlite3_exec(db, sqlcmd, (void *)&getrowid, &rowid, &zErrMsg)!=SQLITE_OK)
error(zErrMsg);
if(!rowid)
error("not found");
snprintf(sqlcmd, 1024, "select type from '%s' where name like '%s';", argv[TABLE], filename);
if(sqlite3_exec(db, sqlcmd, (void *)&gettype, &imgtype, &zErrMsg)!=SQLITE_OK)
error(zErrMsg);
if(!strlen(imgtype) || !(strcasecmp(imgtype, "gif")==0 || strcasecmp(imgtype, "jpg")==0 || strcasecmp(imgtype, "png")==0))
error("wrong extension / img type");
if(sqlite3_blob_open(db, "main", argv[TABLE], "data", rowid, 0, &dblob)!=SQLITE_OK)
error((char*)sqlite3_errmsg(db));
blobsize=sqlite3_blob_bytes(dblob);
if(!blobsize)
error("image is 0 bytes in size");
blobmem=malloc(blobsize);
if(!blobmem)
error("unable to allocate memory");
if(sqlite3_blob_read(dblob, blobmem, blobsize, 0)!=SQLITE_OK)
error("unable to read image");
sqlite3_blob_close(dblob);
snprintf(outname, 1024, "%s.%s", filename, imgtype);
fblob=fopen(outname, "w");
if(fblob==NULL)
error("unable to open target file");
if(fwrite(blobmem, blobsize, 1, fblob)!=1)
error("unable to write to target file");
fclose(fblob);
free(blobmem);
}
else
error("wrong usage");
sqlite3_close(db);
return 0;
}
为了将ID与图像一起存储,只需在同一表中创建另一列即可。
相关文章:
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- 用FWRITE创建图像后,将交换字节
- HTTP post 使用 libcurl 创建 InfluxDB 数据库
- 使用用户输入创建JSON数据库
- 如何在 c++ 程序中创建内部数据库
- 在C++中创建 Sqlite 数据库
- OpenGL纹理格式,为OpenGL创建图像/纹理数据
- 使用IBPP使用C 创建Firebird数据库
- 从命令行创建Intellisense数据库
- 如何在Qt中创建SQLite数据库
- 如何使用 C 编程创建图像数据库
- Imwrite不创建图像
- 我如何在Microsoft Visual c++ 2010 Express项目中创建本地数据库
- 如何从QGraphicsScene/QGraphicsView创建图像文件
- 在Qt (c++)中创建SQLite数据库
- 在qt中创建一个数据库连接类
- 创建用户数据库错误"no match for 'operator==' in....." --C++
- 将示例图像与图像数据库进行比较时出现意外结果 - Opencv c++