【问题标题】:How to convert a bitmap or byte array to an image?如何将位图或字节数组转换为图像?
【发布时间】:2018-08-21 10:20:44
【问题描述】:

我的应用,当用户点击捕捉按钮时,开始相机捕捉。

我使用位图 API。但有时内存问题。 所以我想使用Glide 库。

当前我的来源

//Run on Preview call from Camera
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
  YuvImage image = new YuvImage(data, previewFormat, 640, 480, null);

  Rect rect = new Rect(0, 0, 640, 480);
  ByteArrayOutputStream bao = new ByteArrayOutputStream();
  image.compressToJpeg(rect, 100, bao); //Compress data in YUY2 format into a rectangular area in jpeg format.

  byte[] jpeg = bao.toByteArray();
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inPreferredConfig = Bitmap.Config.RGB_565;

  Bitmap convertedImage = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length, options); //byte array to bitmap

  mCallback.imageTaken(Bitmap.createScaledBitmap(convertedImage, 480, 360, true));
}

imageTaken(CaptureView.class)

   //completed capture picture.
   public void imageTaken(Bitmap bitmap) {
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

      displayImage(bitmap); //preview bitmap image.
   }

displayImage(CaptureView.class);

public class CaptureView extends View implements CaptureViewer {

   private Bitmap mBitmap = null;
   private Rect src = new Rect(120, 96, 342, 272);
   private Rect dst;
   private int viewWidth, viewHeight = 0;

   @Override
   protected void onMeasure(int width, int height) {
      super.onMeasure(width, height);
      viewWidth = getLayoutParams().width;
      viewHeight = getLayoutParams().height;

      dst = new Rect(0, 0, viewWidth, viewHeight);
   }

   @Override
   public void displayImage(Bitmap image) {
      if (mBitmap != null) {
          mBitmap.recycle();
      }
      mBitmap = null;
      mBitmap = Bitmap.createBitmap(image);
      postInvalidate();
  }

  @Override
  protected void onDraw(Canvas canvas) {
     super.onDraw(canvas);
     canvas.drawBitmap(mBitmap, src, dst, null);
  }
}

这个源代码工作,但我想使用 Glide 库。 大多数示例,使用 url glide 库。

但我想要位图到图像滑动库。

如何使用 glide android 将位图或字节数组转换为图像?

【问题讨论】:

  • 看看this
  • @HemantParmar 我检查了你的链接,但我不知道在哪里放置 glide。
  • 位图已经是图像。或者您对图像的确切含义是什么?
  • @HemantParmar 检查此stackoverflow.com/a/42278907/3111083

标签: android android-glide


【解决方案1】:

我认为使用下面的代码可以得到位图

Glide.with(this)
    .asBitmap()
    .load(path)
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
            imageView.setImageBitmap(resource);
        }
    });

【讨论】:

    【解决方案2】:

    你可以试试这个。取自here

         public void imageTaken(Bitmap bitmap) {
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    
       Glide.with(this)
        .load(stream.toByteArray())
        .asBitmap()
        .error(R.drawable.ic_thumb_placeholder)
        .transform(new CircleTransform(this))
        .into(yourImageView); //preview bitmap image.
       }
    

    【讨论】:

    • 我是从您的代码中获取的。 displayImage 是您尝试在其中加载图像的图像视图。查看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 2013-01-14
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多