I would like using HttpClient to invoke a service (localhost/png/ 'imageWithBase64') which will return me answer from the variable 'result'. Ocrad.js reads a basic, simple text with images.
I have 3 questions: - How to send Java is base64 encoded to the service? - And whether paste this id to img.src will work correctly? - The result returned with the form of JSON or HTML format?
Currently, I get a bug in Java (I can not send the entire String as a parameter id to the service)
java.lang.IllegalArgumentException: Illegal character in path
Code Java:
File file = new File(getClass().getClassLoader().getResource("test.png").getFile());
String encodedFile = Base64.encode(FileUtils.readFileToByteArray(file));
String url = "http://127.0.0.1:3000/png/"+encodedFile;
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
My Java Dependency:
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
And server with nodeJS:
var express = require('express');
var OCRAD = require('ocrad.js');
var Canvas = require('canvas');
var Image = Canvas.Image;
var app = express();
app.get('/png/:id', function(req, res) {
var img = new Image();
img.src = 'data:image/png;base64,'+req.params.id;
var canvas = new Canvas(img.width, img.height);
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
var result = OCRAD(canvas);
res.send({result:result});
});
app.listen(3000);
console.log('Listening on port 3000...');