Draw Text on jpeg image from buffer

654 views
Skip to first unread message

mesut...@toros.edu.tr

unread,
Apr 28, 2018, 6:05:08 PM4/28/18
to skia-discuss
Hi, I'm new to skia. I need some answers and a short sample code if possible. I need to write an application to ip camera. I can get the video from cameray in "yuv" and "jpeg" format. I need to write text on the image. 
Before l was try opencv but because of high cpu usage l decide change library. 
my ip camera has arm neon processor and my application will run over ip camera.
how can I do this fastest? what way l must follow? 

video information structure

typedef struct {
     char * buff;
     char * frameTime;
     int size;
     int width;
     int height;
}

thanks for help.

Hal Canary

unread,
Apr 28, 2018, 6:39:10 PM4/28/18
to skia-discuss
Skia can read and write jpeg files, assuming you link in libjpeg-turbo when you build.

Skia will convert to RGB to draw the image. 

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

mesut...@toros.edu.tr

unread,
Apr 29, 2018, 2:43:48 AM4/29/18
to skia-discuss

thank you for quick answer. yes l did build with my minimal needs for ubuntu x86 like this.


bin/gn gen out/x86 --args='is_official_build=false is_debug=false cc="clang" cxx="clang++" target_cpu="x86" target_os="linux" skia_use_libwebp=false skia_use_fontconfig=false skia_use_system_freetype2=false skia_use_egl=false skia_use_lua=false skia_use_libpng=false skia_use_vulkan=false skia_enable_pdf=false skia_use_sfntly=false skia_enable_tools=false skia_use_angle=false skia_use_dng_sdk=false'


when l finish my code l will compile for arm and build my code. 


l try write my code but l thinks some tings wrong, l try read jpeg data and writ text over it , for test trying l write to file jpeg file is 0 kb.



void drawText(void* pData)

{

VIDEO_EVENT* vidEvent;

        vidData = (VIDEO_EVENT*) pData;


sk_sp<SkData> data = SkData::MakeWithCString(pData->buff);

sk_sp<SkImage> img = SkImage::MakeFromEncoded(data);


SkBitmap bitmap;

bitmap.setInfo(SkImageInfo::Make(pData->width, pData->height, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),0);

SkCanvas canvas(bitmap);

canvas.drawImage(img, 100, 50);

SkPaint paint;

SkString string("hello");

canvas.drawString(string, 20, 20, paint);


sk_sp<SkData> encoded = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kJPEG, 100);


// l need put back jpeg data to pData->buff and set pData->size

// for test l write image to file but its write 0 kb.

FILE* pFile = fopen("/home/user/pics/j19.jpg", "wb+");

    if (!pFile) {

  fprintf(stderr, "The file '%s' was not opened\n", "j17.jpg");

  return;

    }

    fwrite(encoded->data(), encoded->size(), 1, pFile);

    fclose(pFile);

}

Hal Canary

unread,
May 1, 2018, 10:28:25 PM5/1/18
to skia-discuss
#include <cassert>
#include <cstdio>
#include "SkCanvas.h"
#include "SkData.h"
#include "SkImage.h"
#include "SkSurface.h"
sk_sp<SkData> drawText(const void* jpegData, size_t jpegDataLen, const char* string) {
    assert(jpegDataLen);
    sk_sp<SkImage> img = SkImage::MakeFromEncoded(
            SkData::MakeWithoutCopy(jpegData, jpegDataLen));
    assert(img);
    sk_sp<SkSurface> surf = SkSurface::MakeRasterN32Premul(
            img->width() + 100, img->height() + 50);
    SkCanvas* canvas = surf->getCanvas();
    canvas->clear(SK_ColorWHITE);
    canvas->drawImage(img, 100, 50);
    SkPaint paint;
    canvas.drawString(string, 20, 20, paint);
    return surf->makeImageSnapshot()->encodeToData(SkEncodedImageFormat::kJPEG, 100);
}
void writeFile(SkData* src, const char* path) {
    FILE* pFile = fopen(path, "wb");
    if (!pFile) {
        fprintf(stderr, "The file '%s' was not opened\n", path);
        return;
    }
    fwrite(src->data(), src->size(), 1, pFile);
    fclose(pFile);
}
void test(VIDEO_EVENT* pData) {
    sk_sp<SkData> encoded = drawText(pData->buff, pData->size, "hello");
    assert(encoded);
    writeFile(encoded.get(), "/home/user/pics/j19.jpg");
}


--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss+unsubscribe@googlegroups.com.

mesut...@toros.edu.tr

unread,
May 4, 2018, 6:37:31 AM5/4/18
to skia-discuss
this sample code has helped me a lot, thank you very much

2 Mayıs 2018 Çarşamba 05:28:25 UTC+3 tarihinde Hal Canary yazdı:
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages