我正在尝试开发一个 OCR 应用程序,它将识别 Camera pic 的内容,但得到一个响应“这个图像太大”,我试图调整图像的大小,但仍然没有发生。我的 OCR 代码是这样的
public class weocr {
String response;
@SuppressWarnings("unused")
private String selectedpath;
weocr(String selectedpath) throws UnsupportedEncodingException, ParseException, ClientProtocolException, IOException
{
String url="http://appsv.ocrgrid.org/cgi-bin/weocr/submit_tesseract.cgi";
response="";
this.selectedpath=selectedpath;
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post=new HttpPost(url);
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
//test start
try {
File imgFile = new File(selectedpath);
int h = 200; // height in pixels
int w = 200; // width in pixels
Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Bitmap bm1 = Bitmap.createScaledBitmap(bm, h, w, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 50, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "testbin.png");
//test end
entity.addPart( "userfile", bab);
// For usual String parameters
entity.addPart( "outputencoding", new StringBody("utf-8"));
entity.addPart( "outputformat", new StringBody("txt"));
post.setEntity( entity );
HttpResponse response = client.execute(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
this.response=new String(s.toString());
//System.out.println("Response: " + s);
// Here we go!
//String response = EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" );
//client.getConnectionManager().shutdown();
//System.out.println(response);
//Toast toast=Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG);
//toast.show();
}
catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
}
}