File: hocr_java.c

package info (click to toggle)
hocr 0.8.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,000 kB
  • ctags: 430
  • sloc: sh: 8,850; ansic: 7,481; makefile: 247; cpp: 242; python: 72; perl: 11; java: 1
file content (87 lines) | stat: -rw-r--r-- 2,679 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <hocr.h>
#include <hocr_pixbuf.h>
#include <assert.h>

#include "hocr_java.h"

/**************************************************************
	public static native String doOcr_Params(
    byte[] pixels,
    int height,
    int width,
    int rowstride, //0=calculate using n_channels,width
    int n_channels, // 1,3
    int brightness, //0=default
    int command //0=default  
  );
**************************************************************/
/*
 * Class:     Hocr_Plugin
 * Method:    doOcr_Params
 * Signature: ([BIIIIII)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_Hocr_1Plugin_doOcr_1Params
  (JNIEnv *env,
   jclass cl,
   jbyteArray pixels,
   jint height,
   jint width,
   jint rowstride,  //0=calculate using n_channels,width
   jint n_channels, // 1,3,
   jint brightness,
   jint command     //0=default
   )   
{
	jstring jstr = NULL;
  hocr_text_buffer *textbuf;
  hocr_pixbuf *pixbuf = hocr_pixbuf_new();
  assert(pixbuf);
	
  pixbuf->n_channels=n_channels; /** number of color channels in the pixpuf (e.g. 3 for red,green,blue). */
  pixbuf->brightness = brightness; /** value from which a gray-scale pixel is considered white. */
  pixbuf->height = height; /** get height in pixels. */
  pixbuf->width = width; /** get width in pixels. */
   
  /** get number of bytes in a raw of pixels. */
  if (rowstride)
    pixbuf->rowstride = rowstride;
  else
  {
    assert(pixbuf->n_channels==3 || pixbuf->n_channels==1);
    pixbuf->rowstride = pixbuf->n_channels * pixbuf->width;
  }
    
  // raw pixpuf data.
  pixbuf->pixels = (unsigned char*)((*env)->GetByteArrayElements(env, pixels, NULL));

  if (!command)
  {
  	// build ocr command 
  	pixbuf->command  = HOCR_COMMAND_OCR;
    pixbuf->command |= HOCR_COMMAND_NIKUD; // use nikud
    pixbuf->command |= HOCR_COMMAND_USE_SPACE_FOR_TAB; // use spaces
    pixbuf->command |= HOCR_COMMAND_USE_INDENTATION; // use indentation
    pixbuf->command |= HOCR_COMMAND_COLOR_BOXES;
    pixbuf->command |= HOCR_COMMAND_COLOR_MISREAD;
  }
  else
    pixbuf->command = command;

	textbuf = hocr_text_buffer_new (); // create text buffer
	assert(textbuf);

	hocr_do_ocr (pixbuf, textbuf); // run the ocr
	
	(*env)->ReleaseByteArrayElements(env, pixels, (signed char*)(pixbuf->pixels), 0);
	pixbuf->pixels = NULL; // hocr_pixbuf_unref() shouldn't free pixel mem
	hocr_pixbuf_unref (pixbuf);  // unref memory
	pixbuf = NULL;

  // create java String object from OCR'ed text
  jstr = (*env)->NewStringUTF(env, textbuf->text);
  
	hocr_text_buffer_unref (textbuf); // unref textbuf

	return jstr;
}