【问题标题】:Trouble in retrieving the JPEG image from inputstream socket in Android从 Android 中的输入流套接字检索 JPEG 图像时遇到问题
【发布时间】:2013-11-30 11:59:57
【问题描述】:

我想从 Android 中的输入流套接字中检索 JPEG 图像。我真的可以使用 Bitmap 方法并直接将输入流转换为 JPG,还是应该将输入流转换为字节数组,然后再转换为 JPG?

我实际尝试的代码是:

            private final BluetoothSocket mmBTSocket;
    private final InputStream mmBTInStream;
    private final OutputStream mmBTOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        Log.d(BTTAG, "create ConnectedThread");
        mmBTSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            Log.e(BTTAG, "temp sockets not created", e);
        }

        mmBTInStream = tmpIn;
        mmBTOutStream = tmpOut;
    }
                      public void run() {

                      Log.i(BTTAG, "BEGIN mBTConnectedThread");

                        byte[] inBTBuffer = new byte[1024];

                         boolean BTFileEndOne = false;

                         byte[] mBTimageBuffer = new byte[15360]; // 15KB reserved

                               int bytes;

                                bytes = mmBTInStream.read(inBTBuffer);

                for (int i = 0; i < bytes; i++) {
                    mBTimageBuffer[mBTfileIndex] = inBTBuffer[i];
                    mBTfileIndex++;
                    // start = System.currentTimeMillis();
                    // Log.i(BTTAG, bytes+"="+String.format("%02X",
                    // inBTBuffer[i]));
                    if (i > 0) {
                        if (inBTBuffer[i] == (byte) 0xD9) {
                             BTFileEndOne = true;
                             Log.i(BTTAG, "BTFileEndOne = true");

                            if (inBTBuffer[i - 1] == (byte) 0xFF) {
                                //if (SaveImagetoSD() == true) {

                                    //byte[] readBuf = (byte[]) msg.obj;
        Bitmap bitmap = BitmapFactory.decodeStream(mmBTInStream);   
        bitmap.compress(CompressFormat.JPEG, 80, mmBTOutStream);

        String writeTo = new File(Environment.getExternalStorageDirectory()+ File.separator + "motoduino.jpg")  .toString();                   
         FileOutputStream output = null;  
         try {
            output = new FileOutputStream(writeTo);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }`

【问题讨论】:

  • 从代码中看起来 mmBTinStream 用于发送多个文件,所以不幸的是 BitmapFactory.decodeStream(mmBTinStream) 无法正常工作,因为它需要查看 jpg 或 png 文件一直作为一个文件。您需要先将字节保存到文件中,这样您还可以测试它没有损坏的内容。
  • 您需要在应用中使用传输的图片,还是只保存而不在应用中使用?
  • 实际上我会将传输的图像上传到服务器。但实际上我只从 arduino 发送一个文件
  • 首先我需要将接收到的字节以.JPG格式保存在SD卡中,然后我将从SD卡上传图像。
  • 我可以将图像保存在 SD 卡中,但我无法查看图像。它说图像坏了??

标签: android bytearray inputstream


【解决方案1】:

BitmapFactory 可以同时加载 png 和 jpg。

Bitmap bitmap = BitmapFactory.decodeStream(inputstream);

您可以保存为 jpeg 或 png 格式:

bitmap.compress(CompressFormat.JPEG, 80, outputstream);

编辑:保存到外部存储:

File sdcard = Environment.getExternalStorageDirectory();
    if(sdcard!=null){
        File myjpg = new File(sdcard,"test.jpg");
        try{
            OutputStream outputstream = new FileOutputStream(myjpg);
            mybitmap.compress(CompressFormat.JPEG, 80, outputstream);
            outputstream.close();
        }catch(Exception e){ e.printStackTrace(); }
    }else{
        Log.e("jpg save","sdcard not inserted or not ready");
    }

您需要在清单中使用 uses-permission android.permission.WRITE_EXTERNAL_STORAGE。

【讨论】:

  • 非常感谢您的评论。我怎样才能将它保存到 SD 卡?
  • 当我以这种方式尝试时,它返回 NULL。 11-17 15:46:03.757: D/skia(21948): --- SkImageDecoder::Factory 返回 null
  • 如果它返回 null 那么我们需要查看您的输入流是如何创建的。
  • 要保存到外部存储,您需要: 在清单中。您需要您的 sdcard 路径: File sdcard = Environment.getExternalStorageDirectory();确保 sdcard!=null 从 sdcard 路径创建一个新文件 File outfile = new File(sdcard,"myfilename.jpg");从该文件创建输出流: OutputStream outputstream = new FileOutputStream(outfile);并将其传递给我原始答案中的 bitmap.compress 行。
  • 我附上了我试图解码的代码。兄弟也请看一下。
猜你喜欢
  • 1970-01-01
  • 2012-11-01
  • 2012-08-17
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多