<commit_before>package de.mpg.mpdl.labcam.Utils;



import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.SparseArray;
import android.graphics.ImageFormat;

import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.MultiDetector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;

import java.nio.ByteBuffer;
import java.util.ArrayList;


/**
 * Created by soultice on 02-Oct-16.
 */

public class OCRtextHandler {

    public static String getText(Context innercontext, Bitmap bitmap) {

        if (bitmap == null){
            Log.e("Error: ", "Bitmap size is zero");
        }

        Log.e("Starting:", "BufferConfiguration");
        int bytes = bitmap.getByteCount();
        ByteBuffer buffer = ByteBuffer.allocate(bytes);
        bitmap.copyPixelsToBuffer(buffer);

        Log.e("Starting:", "FrameBuilder");
        Frame newFrame = new Frame.Builder()
                .setImageData(buffer, bitmap.getWidth(), bitmap.getHeight(), ImageFormat.NV21)
                //.setBitmap(bitmap)
                .build();
        //initialize TextRecognizer and BarcodeDetector
        Log.e("Starting:", "DetectorInit");
        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(innercontext).build();
        TextRecognizer textRecognizer = new TextRecognizer.Builder(innercontext).build();
        MultiDetector multiDetector = new MultiDetector.Builder()
                .add(barcodeDetector)
                .add(textRecognizer)
                .build();

        //Detect text and barcodes in the given frame newFrame
        Log.e("Starting:", "Detectrion");
        SparseArray<Object> objectSparseArray = multiDetector.detect(newFrame);

        multiDetector.release();
        textRecognizer.release();
        barcodeDetector.release();

        Log.e("Starting:", "StringConversion");
        ArrayList<String> fullText = new ArrayList<String>();

        //parse text and barcodes, add them to the arraylist
        for (int i = 0; i < objectSparseArray.size(); i++){
            Object currObject = objectSparseArray.valueAt(i);
            if(currObject instanceof TextBlock){
                Log.e("IS", "TEXTBLOCK");
                TextBlock item = (TextBlock) currObject;
                if (item != null && item.getValue() != null) {
                    fullText.add(item.getValue());
                }
            } else if(currObject instanceof Barcode){
                Log.e("IS","BARCODE");
                Barcode item = (Barcode) currObject;
                if (item != null && item.displayValue != null) {
                    fullText.add(item.displayValue);
                }
            }
        }

        //convert arraylist to string
        StringBuilder sb = new StringBuilder();
        for (String s : fullText) {
            sb.append(s);
            sb.append("\t");
        }

        String message = sb.toString();
        return message;
    }
}<commit_msg>auto detect OCR<commit_after>package de.mpg.mpdl.labcam.Utils;


import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.SparseArray;
import android.widget.Toast;

import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;

/**
 * Created by soultice on 02-Oct-16.
 */

public class OCRtextHandler {

    public static String getText(Context innercontext, Bitmap bitmap) {
        Frame newFrame = new Frame.Builder()
                .setBitmap(bitmap)
                .build();
        //initialize Textrecognizer
        TextRecognizer textRecognizer = new TextRecognizer.Builder(innercontext).build();
        //parse text
        SparseArray<TextBlock> currentText = textRecognizer.detect(newFrame);

        if (!textRecognizer.isOperational()) {
            Log.e(TAG, "Detector dependencies are not yet available.");

            // Check for low storage.  If there is low storage, the native library will not be
            // downloaded, so detection will not become operational.
            IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
//            boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;
//
//            if (hasLowStorage) {
//                Toast.makeText(this, R.string.low_storage_error, Toast.LENGTH_LONG).show();
//                Log.w(TAG, getString(R.string.low_storage_error));
        }
        //Add text to an arraylist
        ArrayList<String> fullText = new ArrayList<String>();
        if (currentText.size() == 0) {
            fullText.add("No text found");
        }
        for (int i = 0; i < currentText.size(); ++i) {
            TextBlock item = currentText.valueAt(i);
            if (item != null && item.getValue() != null) {
                fullText.add(item.getValue());
            }
        }
        //Convert arraylist to stringbuilder
        StringBuilder sb = new StringBuilder();
        for (String s : fullText) {
            sb.append(s);
            sb.append("\t");
        }
        String message = sb.toString();
        return message;
    }
}
{'fraction_non_alphanumeric': 0.1143991338866835, 'fraction_numerical': 0.0023457235654998194, 'mean_word_length': 2.379878048780488, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '136861', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1494, 'n_words': 342}