Android-Ocr using Tesseract in Portrait - android

I used the ocr sample in this link https://github.com/rmtheis/android-ocr
Every thing is working fine but i want it in Portrait view,I followed the steps in this link , Zxing Camera in Portrait mode on Android, to enable ocr tesstow in Portrait mode . The View is portrait now but the camera is still taking the picture in landscape mode.
Any help ?
final class PreviewCallback implements Camera.PreviewCallback {
private static final String TAG = PreviewCallback.class.getSimpleName();
private final CameraConfigurationManager configManager;
private Handler previewHandler;
private int previewMessage;
PreviewCallback(CameraConfigurationManager configManager) {
this.configManager = configManager;
}
void setHandler(Handler previewHandler, int previewMessage) {
this.previewHandler = previewHandler;
this.previewMessage = previewMessage;
}
// (NV21) format.
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
Point cameraResolution = configManager.getCameraResolution();
Handler thePreviewHandler = previewHandler;
if (cameraResolution != null && thePreviewHandler != null) {
Message message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,
cameraResolution.y, data);
message.sendToTarget();
previewHandler = null;
} else {
Log.d(TAG, "Got preview callback, but no handler or resolution available");
}
}

Are you using the preview data with this method:
public void onPreviewFrame(byte[] data, Camera camera) {}
If yes, then I can help you, since I am doing very similar project (that will be open sourced soon)
here is the code that I am using to rotate the preview image
public static Bitmap getBitmapImageFromYUV(byte[] data, int width,
int height, int degree, Rect rect) {
Bitmap bitmap = getBitmapImageFromYUV(data, width, height, rect);
return rotateBitmap(bitmap, degree,rect);
}
public static Bitmap rotateBitmap(Bitmap source, float angle, Rect rect) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
source = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
source.getHeight(), matrix, true);
source = Bitmap.createBitmap(source, rect.left, rect.top, rect.width(), rect.height());
if(mShouldSavePreview)
saveBitmap(source);
return source;
}
public static Bitmap getBitmapImageFromYUV(byte[] data, int width,
int height, Rect rect) {
YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width, height,
null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, width, height), 90, baos);
byte[] jdata = baos.toByteArray();
BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length,
bitmapFatoryOptions);
Log.d(TAG,"getBitmapImageFromYUV w:"+bmp.getWidth()+" h:"+bmp.getHeight());
return bmp;
}

guys i found the solution!
Replace the next code in function: ocrDecode(byte[] data, int width, int height) in DecodeHandler.java file
beepManager.playBeepSoundAndVibrate();
activity.displayProgressDialog();
// *************SHARNOUBY CODE
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width;
width = height;
height = tmp;
//******************************
// Launch OCR asynchronously, so we get the dialog box displayed
// immediately
new OcrRecognizeAsyncTask(activity, baseApi, rotatedData, width, height)
.execute();
...the problem was in the switch case in the function handleMessage(Message message)
the second case was never triggered which calls the rotation code

Related

OutOfMemory Error in processing frames from onPreviewFrame

I am capturing frames in OnPreviewFrame() and then processing them in a thread to check if they are valid or not.
public void onPreviewFrame(byte[] data, Camera camera) {
if (imageFormat == ImageFormat.NV21) {
//We only accept the NV21(YUV420) format.
frameCount++;
if (frameCount > 19 && frameCount % 2 == 0) {
Camera.Parameters parameters = camera.getParameters();
FrameModel fModel = new FrameModel(data);
fModel.setPreviewWidth(parameters.getPreviewSize().width);
fModel.setPreviewHeight(parameters.getPreviewSize().height);
fModel.setPicFormat(parameters.getPreviewFormat());
fModel.setFrameCount(frameCount);
validateFrame(fModel);
}
}
}
In validateFrame(), i submit a ValidatorThread runnable instance to a ThreadPoolExecutor with 4 core and max threads, to process the frames parallelly.
public class ValidatorThread implements Runnable {
private FrameModel frame;
public ValidatorThread(FrameModel fModel) {
frame = fModel;
}
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
processNV21Data();
}
private void processNV21Data() {
YuvImage yuv = new YuvImage(frame.getData(), frame.getPicFormat(),
frame.getPreviewWidth(), frame.getPreviewHeight(), null);
frame.releaseData();
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, frame.getPreviewWidth(), frame.getPreviewHeight()), 100, out);
byte[] bytes = out.toByteArray();
yuv = null;
try {
if (out != null)
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
Bitmap baseBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
bytes = null;
// rotate bitmap
baseBitmap = rotateImage(baseBitmap, frame.getRotation());
//create copy of original bitmap to use later
Bitmap mCheckedBitmap = baseBitmap.copy(Bitmap.Config.ARGB_8888, true);
// convert base bitmap to greyscale for validation
baseBitmap = toGrayscale(baseBitmap);
boolean isBitmapValid = Util.isBitmapValid(baseBitmap);
if (isBitmapValid) {
baseBitmap.recycle();
mCheckedBitmap.recycle();
frame = null;
} else {
baseBitmap.recycle();
mCheckedBitmap.recycle();
frame = null;
}
}
public Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
bmpOriginal.recycle();
return bmpGrayscale;
}
private Bitmap rotateImage(final Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotatedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
source.recycle();
return rotatedBitmap;
}
}
The FrameModel class has such declaration :
public class FrameModel {
private byte[] data;
private int previewWidth;
private int previewHeight;
private int picFormat;
private int frameCount;
public void releaseData() {
data = null;
}
// getters and setters
}
I am getting OutOf Memory error while processing multiple frames.
Can anyone help what memory optimisation does the code need?
You can reduce memory usage if you produce grayscale bitmap from YUV data without going through Jpeg. This will also be significantly faster.
public Bitmap yuv2grayscale(byte[] yuv, int width, int height) {
int[] pixels = new int[width * height];
for (int i = 0; i < height*width; i++) {
int y = yuv[i] & 0xff;
pixels[i] = 0xFF000000 | y << 16 | y << 16 | y;
}
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565);
}
Alternatively, you can create an RGB_565 bitmap without going through int[width*height] pixels array, and manipulate the bitmap pixels in place using NDK.

Scaling the image and setting in Image View reduces image quality and squeezes it

I am tying to make a custom camera and after taking picture I am setting it in image view in the same activity as in which I am setting camera. I have been successful in taking the photos but before setting the image in image view I have to scale it which reduces the image quality. Is there any way to show the real image instead of scaling it?
My images are as below First one is real view of camera which is surface view:
After Taking photo it becomes:
The code I am using is:
Camera.PictureCallback picture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
surface_view.setVisibility(View.INVISIBLE);
setupImageDisplay(data);
}
};
private void setupImageDisplay(byte[] data) {
photo = BitmapFactory.decodeByteArray(data, 0, data.length);
photo = scaleDown(photo, true);//scaling down bitmap
imageview_photo.setImageBitmap(photo); //setting bitmap in imageview
}
public Bitmap scaleDown(Bitmap realImage, boolean filter) {
int screenWidth = width;
int screenHeight = height;
Bitmap scaled;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Notice that width and height are reversed
scaled = Bitmap.createScaledBitmap(realImage, screenHeight, screenWidth, filter);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
if (camera_id == Camera.CameraInfo.CAMERA_FACING_FRONT) {
float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
Matrix matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
mtx.postConcat(matrixMirrorY);
}
mtx.postRotate(90);
// Rotating Bitmap
realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, filter);
} else {// LANDSCAPE MODE
//No need to reverse width and height
scaled = Bitmap.createScaledBitmap(realImage, screenHeight, screenWidth, filter);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
if (camera_id == Camera.CameraInfo.CAMERA_FACING_FRONT) {
float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
Matrix matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
mtx.postConcat(matrixMirrorY);
}
mtx.postRotate(180);
// Rotating Bitmap
realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, filter);
}
return realImage;
}
After taking photo the image is like squeezed is there any way that image remains the same after scaling?
You can create a separate file which is temporary file and stores the thumbnail size of the image. You can make a POJO like this to store both images. You can display the smaller one and use the original file to keep high quality.
public class Image {
File fullSize;
File Thumbnail;
public Image(File fullSize, File thumbnail) {
this.fullSize = fullSize;
Thumbnail = thumbnail;
}
public File getFullSize() {
return fullSize;
}
public void setFullSize(File fullSize) {
this.fullSize = fullSize;
}
public File getThumbnail() {
return Thumbnail;
}
public void setThumbnail(File thumbnail) {
Thumbnail = thumbnail;
}
}

Android camera preview remove green screen?

The user will use the front camera, with a green cloth on the wall (chromatic background )
How is possible to access the preview data and change it: remove the selected color with a tolerance? Ex green, tolerance 65%
#Override
public void onPreviewFrame(final byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] bytes = out.toByteArray();
final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
((ImageView) findViewById(R.id.loopback)).setImageBitmap(bitmap);
}
});
}
http://jylee-world.blogspot.ro/2014/12/a-tutorial-of-androidhardwarec
We need simple tutorials. There is no good sample code even in
http://developer.android.com .
Use Bitmap.getPixel(int x, int y) to get the color of each pixel and bitmap.setPixel() to change the color to what you like
int pixel = bitmap.getPixel(x,y);
int R = Color.red(pixel);
int G = Color.blue(pixel);
int B = Color.green(pixel);
if G == //(range of green values)
bitmap.setPixel(x, y, color)

Scan a code with xzing from a photo

I have a feature request. The current flow is for the user to scan a code (not a QR code, not sure what it is, zxing will scan it), then scan the test card.
The client has asked for me allow the user to import the test from the library. So we need to be able to scan the code off an image.
Is it possible to do this in zxing or am I forced to use the camera / feature is not possible?
Thanks!
Here is my solution. I had to downsize the image, and inver the colors for it to work with zxing. I might add a convert to gray scale, but not today..
public static String scanDataMatrixImage(Bitmap bitmap) {
bitmap = doInvert(bitmap);
double scaling = getScaling(bitmap);
Bitmap resized;
if(scaling>0) {
resized = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * scaling), (int) (bitmap.getHeight() * scaling), true);
}
else{
resized = bitmap;
}
String contents = null;
int[] intArray = new int[resized.getWidth() * resized.getHeight()];
//copy pixel data from the Bitmap into the 'intArray' array
resized.getPixels(intArray, 0, resized.getWidth(), 0, 0, resized.getWidth(), resized.getHeight());
LuminanceSource source = new RGBLuminanceSource(resized.getWidth(), resized.getHeight(), intArray);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
try
{
Result result = reader.decode(binaryBitmap);
contents = result.getText();
} catch (
Exception e
)
{
Log.e("QrTest", "Error decoding barcode", e);
}
return contents;
}
private static double getScaling(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int smallest = width;
if(smallest > height){
smallest = height;
}
double ratio = 200.0/smallest;
return ratio;
}
public static Bitmap doInvert(Bitmap src) {
// create new bitmap with the same settings as source bitmap
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
// color info
int A, R, G, B;
int pixelColor;
// image size
int height = src.getHeight();
int width = src.getWidth();
// scan through every pixel
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// get one pixel
pixelColor = src.getPixel(x, y);
// saving alpha channel
A = Color.alpha(pixelColor);
// inverting byte for each R/G/B channel
R = 255 - Color.red(pixelColor);
G = 255 - Color.green(pixelColor);
B = 255 - Color.blue(pixelColor);
// set newly-inverted pixel to output image
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final bitmap
return bmOut;
}

Android - Fitting bitmap to screen

I have this project in which I have a bitmap bigger than the screen size. I want to resize it to fit the screen exactly. I have no titlebar, and I am in fullscreen mode. This is my non-working code:
public class ScopView extends View
{
private Scop thescop;
public ScopView(Context context, Scop newscop)
{
super(context);
this.thescop = newscop;
}
#Override
public void onDraw(Canvas canvas)
{
Bitmap scopeBitmap;
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false;
bfOptions.inPurgeable = true;
bfOptions.inInputShareable = true;
bfOptions.inTempStorage = new byte[32 * 1024];
scopeBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.scope, bfOptions);
scopeBitmap.createScaledBitmap(scopeBitmap, SniperActivity.Width, SniperActivity.Height, false);
canvas.drawBitmap(scopeBitmap, SniperActivity.scopx, SniperActivity.scopy, null);
}
}
While in here the createScaledBitmap method, I am using itself as the source, and some variables from an activity used to retrieve the window height and width from screen preferences.
You can use the below code to resize the bitmap.
int h = 320; // Height in pixels
int w = 480; // Width in pixels
Bitmap scaled = Bitmap.createScaledBitmap(largeBitmap, h, w, true);
Also, you can use the below code snippet.
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// Create a matrix for the manipulation
Matrix matrix = new Matrix();
// Resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// Recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
This code is help to you try this
int REQ_WIDTH = 0;
int REQ_HEIGHT = 0;
REQ_WIDTH =imageView.getWidth();
vREQ_HEIGHT =imageView.getHeight();
mImageView.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(imageURI, options), REQ_HEIGHT, REQ_WIDTH, true));

Categories

Resources