如何将二进制数据转换为图像?

发布于 2024-12-25 00:23:38 字数 240 浏览 1 评论 0原文

在我的安卓应用程序中。我从代码中的 jpeg 图像中获取了二进制代码,如下所示。

byte[] val = stream.toByteArray();
          BigInteger bi = new BigInteger(val);
    String s =  bi.toString(2);

该字符串 s 打印图像的二进制值。 我的问题是如何将此二进制格式转换为 jpeg 图像?

In my android application. I got binary code from a jpeg image from the code as follows.

byte[] val = stream.toByteArray();
          BigInteger bi = new BigInteger(val);
    String s =  bi.toString(2);

This string s prints the binary value of the image.
My question is how to convert this binary format into a jpeg image??

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

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

发布评论

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

评论(4

心如荒岛 2025-01-01 00:23:38

我不太确定你想要什么。

  • 如果您想直接从流创建 Bitmap 实例,您可以使用 BitmapFactory 并在 中显示该 Bitmap ImageView-之后的实例:

    位图图像 = BitmapFactory.decodeStream(stream);
    imageView.setImageBitmap(图像);
    
  • 如果您想将基数为 2 的字符串表示形式转换回二进制数组,您也可以使用 BigInteger

    BigInteger bigInt = new BigInteger(s, 2);
    byte[] binaryData = bigInt.toByteArray();
    

I'm not really sure what you want.

  • If you want to create a Bitmap-instance directly from the stream you can use BitmapFactory and display that Bitmap in an ImageView-instance afterwards:

    Bitmap image = BitmapFactory.decodeStream(stream);
    imageView.setImageBitmap(image);
    
  • If you want to convert your string representation with radix 2 back to a binary array you can use BigInteger too:

    BigInteger bigInt = new BigInteger(s, 2);
    byte[] binaryData = bigInt.toByteArray();
    
请别遗忘我 2025-01-01 00:23:38
Bitmap bmp=BitmapFactory.decodeByteArray(val, 0, val.length);
ImageView img = new ImageView(this);
img.setImageBitmap(bmp);

希望这有助于

编辑:
写入内部存储器

FileOutputStream fout;
fout = openFileOutput("temp.jpg",Context.MODE_WORLD_WRITEABLE);    
b1.compress(CompressFormat.JPEG, 100, fout);

写入外部存储器

FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/temp.JPEG");
 bm.compress(Bitmap.CompressFormat.JPEG,90, fout);
Bitmap bmp=BitmapFactory.decodeByteArray(val, 0, val.length);
ImageView img = new ImageView(this);
img.setImageBitmap(bmp);

Hope this Helps

Edit:
To write in Internal Memory

FileOutputStream fout;
fout = openFileOutput("temp.jpg",Context.MODE_WORLD_WRITEABLE);    
b1.compress(CompressFormat.JPEG, 100, fout);

To write in External Memory

FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/temp.JPEG");
 bm.compress(Bitmap.CompressFormat.JPEG,90, fout);
平生欢 2025-01-01 00:23:38
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
savefile(bMap,"test.jpg");

private void savefile(Bitmap bt,String file)
    {
        try {
            ContextWrapper context=this;
            FileOutputStream stream =context.openFileOutput(file, 2);
            BufferedOutputStream  objectOut = null;
              //    FileOutputStream stream =(FileOutputStream) getAssets().open("temp.txt");
                  try {
                    objectOut = new BufferedOutputStream(stream);
                     bt.compress(Bitmap.CompressFormat.PNG, 100, objectOut);
                     objectOut.flush(); 
                     objectOut.close();
                  }
                  catch (Exception e) {
                   // TODO Auto-generated catch block

                      }

             } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block

                  }


    }
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
savefile(bMap,"test.jpg");

private void savefile(Bitmap bt,String file)
    {
        try {
            ContextWrapper context=this;
            FileOutputStream stream =context.openFileOutput(file, 2);
            BufferedOutputStream  objectOut = null;
              //    FileOutputStream stream =(FileOutputStream) getAssets().open("temp.txt");
                  try {
                    objectOut = new BufferedOutputStream(stream);
                     bt.compress(Bitmap.CompressFormat.PNG, 100, objectOut);
                     objectOut.flush(); 
                     objectOut.close();
                  }
                  catch (Exception e) {
                   // TODO Auto-generated catch block

                      }

             } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block

                  }


    }
望她远 2025-01-01 00:23:38

我过去使用过这段代码:

InputStream is = (InputStream)imageContent;
d = Drawable.createFromStream(is, "src");

I've used this code in the past:

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