如何将可绘制位读取为输入流

发布于 2024-11-15 11:18:54 字数 63 浏览 2 评论 0原文

有一些 ImageView 对象。我想读取这个对象的位/原始数据作为输入流。怎么做呢?

There's say some ImageView object. I want to read bits/raw data of this object as InputStream. How to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱&纷飞 2024-11-22 11:18:54

首先获取 ImageView 的背景图像作为 Drawable 的对象:

iv.getBackground();

Drawable 图像转换为 Bitmap

BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();

然后使用Now use ByteArrayOutputStreamBitmap 获取到Stream 并获取bytearray[];然后
bytearray 转换为 ByteArrayInputStream

您可以使用以下代码从ImageView获取InputStream

完整源代码

ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d = iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
System.out.println("........length......" + imageInByte);
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);

谢谢
迪帕克

First get background image of the ImageView as an object of Drawable:

iv.getBackground();

Then convert Drawable image into Bitmap using

BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();

Now use ByteArrayOutputStream to get the Bitmap into a Stream and get bytearray[]; then
convert the bytearray into a ByteArrayInputStream.

You can use the following code to get InputStream from ImageView.

Full Source code

ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d = iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
System.out.println("........length......" + imageInByte);
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);

Thanks
Deepak

Saygoodbye 2024-11-22 11:18:54

下面的这些方法非常有用,因为它们适用于任何类型的 Drawable(不仅仅是 BitmapDrawable)。如果您想按照 David Caunt 的建议使用绘图缓存,请考虑使用 bitmapToInputStream 而不是 bitmap.compress,因为它应该更快。

public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

public static InputStream bitmapToInputStream(Bitmap bitmap) {
    int size = bitmap.getHeight() * bitmap.getRowBytes();
    ByteBuffer buffer = ByteBuffer.allocate(size);
    bitmap.copyPixelsToBuffer(buffer);
    return new ByteArrayInputStream(buffer.array());
}

These methods below are useful because they work with any kind of Drawable (not only BitmapDrawable). If you want to use drawing cache as in David Caunt's suggestion, consider using bitmapToInputStream instead of bitmap.compress, because it should be faster.

public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

public static InputStream bitmapToInputStream(Bitmap bitmap) {
    int size = bitmap.getHeight() * bitmap.getRowBytes();
    ByteBuffer buffer = ByteBuffer.allocate(size);
    bitmap.copyPixelsToBuffer(buffer);
    return new ByteArrayInputStream(buffer.array());
}
梦旅人picnic 2024-11-22 11:18:54

您可以使用绘图缓存来检索任何 View 类的位图表示形式。

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();

然后您可以将位图写入 OutputStream,例如:

b.compress(CompressFormat.JPEG, 80, new FileOutputStream("/view.jpg"));

在您的情况下,我认为您可以使用 ByteArrayOutputStream 获取一个字节[],您可以从中创建一个输入流。代码会是这样的:

ByteArrayOutputStream os = new ByteArrayOutputStream(b.getByteCount());
b.compress(CompressFormat.JPEG, 80, os);
byte[] bytes = os.toByteArray();

You can use the drawing cache to retrieve a Bitmap representation of any View class.

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();

Then you can write the bitmap to an OutputStream, for example:

b.compress(CompressFormat.JPEG, 80, new FileOutputStream("/view.jpg"));

In your case I think you can use a ByteArrayOutputStream to get a byte[] from which you can create an InputStream. The code would be something like this:

ByteArrayOutputStream os = new ByteArrayOutputStream(b.getByteCount());
b.compress(CompressFormat.JPEG, 80, os);
byte[] bytes = os.toByteArray();
阳光下慵懒的猫 2024-11-22 11:18:54

您可能正在寻找这个:
openRawResource

You might be looking for this:
openRawResource

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文