Java tutorial
/* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.guanqing.hao; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.support.annotation.NonNull; import android.support.v4.util.ArrayMap; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.vision.text.TextBlock; import com.guanqing.hao.kehan_dict.R; import com.guanqing.hao.ui.camera.CameraSourcePreview; import com.guanqing.hao.ui.camera.GraphicOverlay; import java.util.Locale; import io.reactivex.functions.Action; import io.reactivex.functions.Consumer; import io.reactivex.internal.observers.CallbackCompletableObserver; /** * Activity for the Ocr Detecting app. This app detects text and displays the value with the * rear facing camera. During detection overlay graphics are drawn to indicate the position, * size, and contents of each TextBlock. */ public final class OcrCaptureActivity extends AppCompatActivity { private static final String TAG = "OcrCaptureActivity"; private View mDimView; private GraphicOverlay<OcrGraphic> mGraphicOverlay; private CameraSourceHelper mCameraSourceHelper; private Dict mDict; // Helper objects for detecting taps and pinches. private ScaleGestureDetector scaleGestureDetector; private GestureDetector gestureDetector; // A TextToSpeech engine for speaking a String value. private TextToSpeech tts; /** * Initializes the UI and creates the detector pipeline. */ @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.ocr_capture); init(); } private void init() { mDimView = findViewById(R.id.dim_view); final CameraSourcePreview preview = (CameraSourcePreview) findViewById(R.id.preview); mGraphicOverlay = (GraphicOverlay<OcrGraphic>) findViewById(R.id.graphicOverlay); mDict = new Dict(this); mCameraSourceHelper = new CameraSourceHelper(this, preview, mGraphicOverlay, mDict); gestureDetector = new GestureDetector(this, new CaptureGestureListener()); scaleGestureDetector = new ScaleGestureDetector(this, mCameraSourceHelper); // Set up the Text To Speech engine. TextToSpeech.OnInitListener listener = new TextToSpeech.OnInitListener() { @Override public void onInit(final int status) { if (status == TextToSpeech.SUCCESS) { tts.setLanguage(Locale.US); } } }; tts = new TextToSpeech(this.getApplicationContext(), listener); } @Override public boolean onTouchEvent(MotionEvent e) { boolean b = scaleGestureDetector.onTouchEvent(e); boolean c = gestureDetector.onTouchEvent(e); return b || c || super.onTouchEvent(e); } @Override protected void onResume() { super.onResume(); mDimView.setVisibility(View.GONE); mCameraSourceHelper.startCameraSource(); } @Override protected void onPause() { super.onPause(); mCameraSourceHelper.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mCameraSourceHelper.onDestroy(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); mCameraSourceHelper.onRequestPermissionsResult(requestCode, grantResults); } /** * onTap is called to speak the tapped TextBlock, if any, out loud. * * @param rawX - the raw position of the tap * @param rawY - the raw position of the tap. * @return true if the tap was on a TextBlock */ private boolean onTap(float rawX, float rawY) { OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY); TextBlock text = null; if (graphic != null) { text = graphic.getTextBlock(); if (text != null && text.getValue() != null) { // Speak the string. tts.speak(text.getValue(), TextToSpeech.QUEUE_ADD, null, "DEFAULT"); } } return text != null; } private class CaptureGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapConfirmed(MotionEvent e) { return onTap(e.getRawX(), e.getRawY()) || super.onSingleTapConfirmed(e); } } }