2

我写了一个代码来了解金钱的价值。我正在使用 Mobile Vision 的 OCR 来获取数字和单词,然后如果匹配(我设置了一些条件),应用程序将播放该值的声音。现在我想做一些实验。我希望这个应用程序裁剪数字周围的区域并将其显示在我的活动中。这就是我所做的。

public class MainActivity extends AppCompatActivity {

    SurfaceView cameraView;
    TextView textView;
    CameraSource cameraSource;
    final int RequestCameraPermissionID = 1001;
    String nominal = "";
    String bilangan = "";
    TextToSpeech tts;


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case RequestCameraPermissionID: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    try {
                        cameraSource.start(cameraView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
            break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cameraView = (SurfaceView) findViewById(R.id.surface_view);
        textView = (TextView) findViewById(R.id.text_view);
        tts=new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                // TODO Auto-generated method stub
                if(status == TextToSpeech.SUCCESS){
                    int result=tts.setLanguage(Locale.US);

                    if(result==TextToSpeech.LANG_MISSING_DATA ||
                            result==TextToSpeech.LANG_NOT_SUPPORTED){
                        Log.e("error", "This Language is not supported");
                    }
                    else{
                        ConvertTextToSpeech();
                    }
                }
                else
                    Log.e("error", "Initilization Failed!");
            }
        });

        TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
        if (!textRecognizer.isOperational()) {
            Log.w("MainActivity", "Detector dependencies are not yet available");
        } else {

            cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
                    .setFacing(CameraSource.CAMERA_FACING_BACK)
                    .setRequestedPreviewSize(1280, 1024)
                    .setRequestedFps(2.0f)
                    .setAutoFocusEnabled(true)
                    .build();
            cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder surfaceHolder) {

                    try {
                        if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.CAMERA},
                                    RequestCameraPermissionID);
                            return;
                        }
                        cameraSource.start(cameraView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

                }

                @Override
                public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                    cameraSource.stop();
                }
            });


            textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
                @Override
                public void release() {

                }

                @Override
                public void receiveDetections(Detector.Detections<TextBlock> detections) {

                    final SparseArray<TextBlock> items = detections.getDetectedItems();
                    if(items.size() != 0)
                    {
                        textView.post(new Runnable() {
                            @Override
                            public void run() {
                                StringBuilder stringBuilder = new StringBuilder();
                                for(int i =0;i<items.size();++i)
                                {
                                    TextBlock item = items.valueAt(i);
                                    /*stringBuilder.append(item.getValue());
                                    stringBuilder.append("\n");*/


                                    if (item.getValue().toLowerCase().contains("seribu")){
                                        bilangan = "seribu rupiah";
                                    }
                                    else if(item.getValue().toLowerCase().contains("dua ribu")){
                                        bilangan = "dua ribu rupiah";
                                    }
                                    else if(item.getValue().toLowerCase().contains("lima ribu")){
                                        bilangan = "lima ribu rupiah";
                                    }
                                    else if(item.getValue().toLowerCase().contains("sepuluh ribu")){
                                        bilangan = "sepuluh ribu rupiah";
                                    }
                                    else if(item.getValue().toLowerCase().contains("dua puluh ribu")){
                                        bilangan = "dua puluh ribu rupiah";
                                    }
                                    else if(item.getValue().toLowerCase().contains("lima puluh ribu")){
                                        bilangan = "lima puluh ribu rupiah";
                                    }
                                    else if(item.getValue().toLowerCase().contains("seratus ribu")){
                                        bilangan = "seratus ribu rupiah";
                                    }


                                    if(item.getValue().equals("1000")){
                                        nominal = "1000";
                                    }
                                    else if(item.getValue().equals("2000")){
                                    }
                                    else if(item.getValue().equals("5000")){
                                        nominal = "5000";
                                    }
                                    else if(item.getValue().equals("10000")){
                                        nominal = "10000";
                                    }
                                    else if(item.getValue().equals("20000")){
                                        nominal = "20000";
                                    }
                                    else if(item.getValue().equals("50000")){
                                        nominal = "50000";
                                    }
                                    else if(item.getValue().equals("100000")){
                                        nominal = "100000";
                                    }

                                    stringBuilder.append(bilangan);
                                    stringBuilder.append(nominal);

                                }

                                //textView.setText(stringBuilder.toString());
                                if (!bilangan.equals("") && !nominal.equals("")){
                                    cameraSource.stop();
                                    ConvertTextToSpeech();


                                }

                            }
                        });
                    }
                }
            });
        }

    }

    private void ConvertTextToSpeech() {
        tts.speak(bilangan,TextToSpeech.QUEUE_FLUSH,null,"Niel");

    }
}

只是如何像下面这张图片一样裁剪数字(红色框),然后我将使用 ImageView 在我的活动中显示它。 红色的 谢谢

4

0 回答 0