Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <ImageView
- android:id="@+id/imageId"
- android:layout_width="match_parent"
- android:layout_height="372dp" />
- <Button
- android:id="@+id/buttonId"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="take pic"/>
- <!-- android:onClick="doProcess" -->
- <TextView
- android:id="@+id/textId"
- android:layout_width="match_parent"
- android:layout_height="221dp" />
- </LinearLayout>
- -----------------------------AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="com.example.mlkit02">
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.CAMERA"/>
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:dataExtractionRules="@xml/data_extraction_rules"
- android:fullBackupContent="@xml/backup_rules"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.Mlkit02"
- android:requestLegacyExternalStorage="true"
- tools:targetApi="29">
- <activity
- android:name=".MainActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- -----------------------------build.gradle
- plugins {
- id 'com.android.application'
- }
- android {
- compileSdk 32
- defaultConfig {
- applicationId "com.example.mlkit02"
- minSdk 23
- targetSdk 32
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
- }
- }
- compileOptions {
- sourceCompatibility JavaVersion.VERSION_1_8
- targetCompatibility JavaVersion.VERSION_1_8
- }
- }
- dependencies {
- implementation 'androidx.appcompat:appcompat:1.4.2'
- implementation 'com.google.android.material:material:1.6.1'
- implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
- implementation 'com.google.android.gms:play-services-mlkit-text-recognition:18.0.0'
- testImplementation 'junit:junit:4.13.2'
- androidTestImplementation 'androidx.test.ext:junit:1.1.3'
- androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
- }
- -----------------------------MainActivity.java
- package com.example.mlkit02;
- import androidx.activity.result.ActivityResult;
- import androidx.activity.result.ActivityResultCallback;
- import androidx.activity.result.ActivityResultLauncher;
- import androidx.activity.result.contract.ActivityResultContracts;
- import androidx.annotation.NonNull;
- import androidx.annotation.RequiresApi;
- import androidx.appcompat.app.AppCompatActivity;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Camera;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.google.android.gms.tasks.OnFailureListener;
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.android.gms.tasks.Task;
- import com.google.android.gms.tasks.Tasks;
- import com.google.mlkit.vision.common.InputImage;
- import com.google.mlkit.vision.text.Text;
- import com.google.mlkit.vision.text.TextRecognition;
- import com.google.mlkit.vision.text.TextRecognizer;
- import com.google.mlkit.vision.text.latin.TextRecognizerOptions;
- import java.util.concurrent.ExecutionException;
- public class MainActivity extends AppCompatActivity {
- ImageView imageView;
- TextView textView;
- Button pictButton;
- @RequiresApi(api = Build.VERSION_CODES.M)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = findViewById(R.id.imageId);
- textView = findViewById(R.id.textId);
- pictButton = findViewById(R.id.buttonId);
- if (checkSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_DENIED ||
- checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED ||
- checkSelfPermission(Manifest.permission.INTERNET) == PackageManager.PERMISSION_DENIED ||
- checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED ||
- checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
- //permission not enabled, request it
- String[] permission = {Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.INTERNET, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
- requestPermissions(permission, 1000);
- }
- pictButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //Camera camera = getAndroidCamera();
- Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- someActivityResultLauncher.launch(cameraIntent);
- }
- });
- }
- ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
- new ActivityResultContracts.StartActivityForResult(),
- new ActivityResultCallback<ActivityResult>() {
- @Override
- public void onActivityResult(ActivityResult result) {
- if (result.getResultCode() == Activity.RESULT_OK) {
- Intent data = result.getData();
- Bundle bundle = data.getExtras();
- Bitmap bitmap = (Bitmap) bundle.get("data");
- imageView.setImageBitmap(bitmap);
- String output = null;
- try {
- output = new OCR(imageView).execute().get(); //"/storage/emulated/0/Download/sticker01.jpg"
- } catch (ExecutionException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Log.i("===============OCR==RESULT======================>", output);
- textView.setText(output);
- }
- }
- });
- }
- -----------------------------OCR.java
- package com.example.mlkit02;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.AsyncTask;
- import android.os.SystemClock;
- import android.util.Log;
- import android.widget.ImageView;
- import androidx.annotation.NonNull;
- import com.google.android.gms.tasks.OnFailureListener;
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.android.gms.tasks.Task;
- import com.google.android.gms.tasks.Tasks;
- import com.google.mlkit.vision.common.InputImage;
- import com.google.mlkit.vision.text.Text;
- import com.google.mlkit.vision.text.TextRecognition;
- import com.google.mlkit.vision.text.TextRecognizer;
- import com.google.mlkit.vision.text.latin.TextRecognizerOptions;
- import java.lang.ref.WeakReference;
- public class OCR extends AsyncTask<String, Void, String> {
- private ImageView mImgView;
- public OCR(ImageView image) {
- //WeakReference<ImageView> mImageView = new WeakReference<ImageView>(image);
- mImgView = image;
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected String doInBackground(String... params) {
- String resultText = null;
- try {
- //ImageView imgView = (ImageView) findViewById(R.id.params[0]);
- BitmapDrawable drawable = (BitmapDrawable) mImgView.getDrawable();
- Bitmap bitmap = drawable.getBitmap();
- //Bitmap bitmap = BitmapFactory.decodeFile(params[0]);//"/storage/emulated/0/Download/wp01.png" //"/storage/emulated/0/DCIM/Camera/1659023147826.jpg"
- TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
- InputImage mInputImage = InputImage.fromBitmap(bitmap, 0);
- Task<Text> result =
- recognizer.process(mInputImage).addOnSuccessListener(new OnSuccessListener<Text>() {
- //@Override
- public void onSuccess(Text visionText) {
- }
- })
- .addOnFailureListener(
- new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- }
- });
- Tasks.await(result);
- resultText = result.getResult().getText();
- //Log.i("RESULT ======================>", resultText);
- } catch (Exception e) {
- e.printStackTrace();
- Log.e("Task failed", e.getMessage());
- }
- return resultText;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement