【问题标题】:Using bitmap to take image from imageview使用位图从图像视图中获取图像
【发布时间】:2018-01-31 20:10:18
【问题描述】:

我在使用位图从 imageview 中获取图像时遇到问题...使用位图通过其 ID 获取图像的代码是:(my picture name = test_image)(my imageview = imageView)

Bitmap image;
...
image = BitmapFactory.decodeResource(getResources(), R. drawable.test_image);

但我希望它在 imageview 中获取图像。不指定图像名称。

由于我想将图像上传到ima​​geview并处理imageview中的图像。

【问题讨论】:

    标签: android


    【解决方案1】:

    BitmapDrawable 可绘制 = (BitmapDrawable) imageView.getDrawable(); 位图bitmap = drawable.getBitmap();

    这是从任何图像视图中获取位图的方法

    【讨论】:

      【解决方案2】:
      private Bitmap getImageViewFromBitmap(ImageView v){
          Bitmap bm=((BitmapDrawable)v.getDrawable()).getBitmap();
          return bm;
      }
      

      【讨论】:

      • 它被投射到 BitmapDrawable 中,而不是假设。
      • 我应该把这段代码放在哪里?是在 onCreate 里面吗?
      • anotherimageview.setImageBitmap(getImageViewFromBitmap(imageview));
      【解决方案3】:
      private Bitmap drawableToBitmap (Drawable drawable) {
          if (drawable instanceof BitmapDrawable) {
              return ((BitmapDrawable)drawable).getBitmap();
          }
      
          int width = drawable.getIntrinsicWidth();
          width = width > 0 ? width : 1;
          int height = drawable.getIntrinsicHeight();
          height = height > 0 ? height : 1;
      
          Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(bitmap); 
          drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
          drawable.draw(canvas);
      
          return bitmap;
      }
      

      调用这个函数如下:

      Drawable drawable = imageView.getDrawable();
      Bitmap bitmap = drawableToBitmap(drawable);
      

      【讨论】:

      • 对不起,我是这方面的初学者,我应该在哪里调用该函数?在我的 onCreate 里面?
      • ImageView 初始化后可以从 onCreate() 调用该函数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      相关资源
      最近更新 更多