Giter Site home page Giter Site logo

escpos-printer's Introduction

ESC/POS Printer Library

This is a C library for operating thermal printers by sending ESC/POS commands.

Compiling

make

This will create dist folder, which contains libescposprinter.a and some public header files.

Sample Usage

This sample uses stb_image.h from nothings/stb:

#include <stdio.h>
#include <string.h>
#include "escpos_printer.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

// Load and convert image to grayscale
unsigned char *load_image(const char * const image_path,
                          int *width,
                          int *height)
{
    int w, h, comp;
    unsigned char *image_data = stbi_load(image_path, &w, &h, &comp, 0);

    unsigned char *gs_image_data = (unsigned char *)malloc(sizeof(unsigned char) * w * h);
    if (comp == 1) {
        memcpy(gs_image_data, image_data, sizeof(unsigned char) * w * h);
    } else if (comp == 2) {
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                gs_image_data[y * w + x] = image_data[(y * w * 2) + (x * 2)];
            }
        }
    } else if (comp >= 3) {
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                unsigned char r = image_data[(y * w * comp) + (x * comp)];
                unsigned char g = image_data[(y * w * comp) + (x * comp) + 1];
                unsigned char b = image_data[(y * w * comp) + (x * comp) + 2];

                gs_image_data[y * w + x] = (r + g + b) / 3;
            }
        }
    }

    stbi_image_free(image_data);

    *width = w;
    *height = h;
    return gs_image_data;
}

int main(int argc, char **argv)
{
    escpos_printer *printer = escpos_printer_network("192.168.0.123", 9100);

    if (printer != NULL) {
        int w, h;
        unsigned char *image_data = load_image("image.jpg", &w, &h);

        escpos_printer_image(printer, image_data, w, h);
        escpos_printer_feed(printer, 6);
        escpos_printer_cut(printer);

        free(image_data);
        escpos_printer_destroy(printer);
    } else {
        escpos_error err = escpos_last_error();
        printf("error: %d\n", err);
    }

    return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.