简体   繁体   English

从 ImageView 而不是 Drawable 获取图像

[英]Getting image from ImageView instead of Drawable

Please Note that I am a NEWBIE.请注意,我是新手。 Under Learning Process学习过程中

Right now what this code is doing?现在这段代码在做什么? It is reading an image from drawable and then getting the text from it using google vision.它正在从 drawable 读取图像,然后使用谷歌视觉从中获取文本。

 Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.changing);
        TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

        if(!textRecognizer.isOperational()){
            Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Frame frame = new Frame.Builder().setBitmap(bitmap).build();

            SparseArray<TextBlock> item = textRecognizer.detect(frame); //Yeh Frame Detect Kr rha he

            StringBuilder sb = new StringBuilder();
            for(int i = 0 ; i<item.size();i++){
                TextBlock myitem = item.valueAt(i);
                sb.append(myitem.getValue());
                sb.append("\n");
            }

NOTE: R.drawable.changing is where "changing" is my image name.注意: R.drawable.change 是“改变”是我的图像名称。 Now i want to replace the image with the image i uploaded on ImageView.现在我想用我在 ImageView 上上传的图像替换图像。

Any help would work.任何帮助都会起作用。

You can simply use ImageView.getDrawable() to get the image from an ImageView.您可以简单地使用ImageView.getDrawable()从 ImageView 获取图像。 However, the drawable is not necessarily a BitmapDrawable (I think your changing drawable is).但是,drawable 不一定是 BitmapDrawable(我认为您changing drawable 是)。

To get an actual bitmap of the ImageView's drawable, you might have to look a bit further, on how to create a bitmap from an Android drawable.要获得 ImageView 可绘制对象的实际位图,您可能需要进一步了解如何从 Android 可绘制对象创建位图。 There are many answers to that in StackOverflow, so I will not repeat that here. StackOverflow 中有很多答案,这里不再赘述。

Basically, it's creating a Bitmap and a Canvas and then let the Drawable draw itself into the Bitmap, via the Canvas.基本上,它创建了一个位图和一个画布,然后让 Drawable 通过画布将自己绘制到位图中。

You can get the Bitmap from imageview like this您可以像这样从 imageview 获取Bitmap

Drawable dr = ((ImageView) imView).getDrawable();
Bitmap bmp =  ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();

if you want to get image from drawable you can try this :如果你想从 drawable 获取图像,你可以试试这个:

imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

if you are getting image from resource like gallery:如果您从图库等资源获取图像:

if (requestCode == SELECT_FILE)
    {
        Uri selectedImageUri = data.getData();
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
            imageview.setImageBitmap(bitmap);
    }

Another option for getting image is from camera:获取图像的另一种选择是从相机:

if(requestCode == REQUEST_CAMERA)
        {
            Bundle bundle = data.getExtras();
            final Bitmap bmp = (Bitmap) bundle.get("data");
            imageview.setImageBitmap(bmp);
        }

where SELECT_FILE & REQUEST_CAMERA is just an integer:其中 SELECT_FILE & REQUEST_CAMERA 只是一个整数:

int SELECT_FILE = 0;
int REQUEST_CAMERA = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM