从图库中选择图片并使用Google API提供文字(BitmapFactory.decompileFile null error)

时间:2017-06-26 21:33:06

标签: java android bitmap

我正在制作文字识别应用。我需要从图库中选择一个图像并从该图像中获取文本。

我正在使用此库:com.google.android.gms:play-services-vision:11.0.1

我的代码在我使用

时有效
final Bitmap bitmap = new BitmapFactory(

getApplicationContext()。getResurce(),R.drawable,image)

但我需要从图库中选择图片。

这是我的代码:

public class MainActivity extends AppCompatActivity {

  private static int RESULT_LOAD_IMG = 1;
  Button button;
  TextView textView;
  ImageView imageView;

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

    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMG);

    button = (Button) findViewById(R.id.button);
    textView = (TextView) findViewById(R.id.textView);
    imageView = (ImageView) findViewById(R.id.imageView);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextRecognizer textRecognizer = new TextRecognizer.Builder((getApplicationContext())).build();
            if (!textRecognizer.isOperational()) {
                Log.w("ERROR", "Decetor are not yet available");
            } else {

                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<TextBlock> items = textRecognizer.detect(frame);
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < items.size(); i++) {
                    TextBlock item = items.valueAt(i);
                    stringBuilder.append(item.getValue());
                    stringBuilder.append("\n");
                }
                textView.setText(stringBuilder.toString());
            }
        }
    });

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        try {
            final Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));
            imageView.setBitmapImage(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
  }    
}

Logcat:

  

进程:com.example.dusan.testphotolatorproject,PID:27608 java.lang.NullPointerException:尝试调用虚方法&#39; int android.graphics.Bitmap.getWidth()&#39; com.google.android.gms.vision.Frame $ Builder.setBitmap上的空对象引用(com.example.dusan.testphotolatorproject.MainActivity $ 1.onClick(MainActivity.java:78)中的未知来源

1 个答案:

答案 0 :(得分:0)

删除此垃圾:

        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        final Bitmap bitmap = BitmapFactory.decodeFile(picturePath);

将其替换为:

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        final Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));

此外:

  • 请勿为findViewById()致电ImageView两次,正如您现在所做的那样

  • 不要像现在一样打电话setImageBitmap()两次

  • 将此逻辑移至后台线程,或使用图像加载库(例如,Picasso,Glide),因为您正在主应用程序线程上执行磁盘I / O和位图处理