字节数组到2D数组

时间:2017-03-23 13:06:27

标签: android arrays

我的代码做了什么。拍摄地图的快照,将其转换为灰色(opencv),然后将其转换为字节数组。 现在,我不知道如何开始做这个Bytearray到2D数组,

这是一段代码。

                    Date now = new Date();
                    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss",now);
                    try {
                        StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
                        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(old)
                                .permitDiskWrites()
                                .build());
                        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
                        File imageFile = new File(mPath);
                        FileOutputStream out = new FileOutputStream(imageFile);
                        bitmap.compress(Bitmap.CompressFormat.PNG,90,out);
                        Log.d("Image:","Saved Snashot. Starting covertion");
                        //show snapshot in imageview
                        ImageView imgview = (ImageView) findViewById(R.id.imageView);
                        Bitmap smyBitmap = BitmapFactory.decodeFile(mPath);
                        Bitmap myBitmap = BitmapFactory.decodeFile(mPath);
                        imgview.setImageBitmap(smyBitmap);
                        Mat mat = new Mat(myBitmap.getHeight(),myBitmap.getWidth(),CvType.CV_8UC3);
                        Mat mat1 = new Mat(myBitmap.getHeight(),myBitmap.getWidth(),CvType.CV_8UC1);
                        Imgproc.cvtColor(mat,mat1,Imgproc.COLOR_BGR2GRAY);
                        ImageView imgview2 = (ImageView) findViewById(R.id.imageView2);
                            Mat tmp = new Mat (myBitmap.getWidth(), myBitmap.getHeight(), CvType.CV_8UC1);
                            Utils.bitmapToMat(myBitmap, tmp);
                            Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
                            Utils.matToBitmap(tmp, myBitmap);
                          //  Utils.matToBitmap(mat1,img);
                        String mPathgray = Environment.getExternalStorageDirectory().toString() + "/" + now + "gray.jpg";
                        File imageFilegray = new File(mPathgray);
                        FileOutputStream gout = new FileOutputStream(imageFilegray);

                        bitmap.compress(Bitmap.CompressFormat.PNG,90,gout);

                        byte[] byteArray = bttobyte(myBitmap);
                        Log.d("location"," " + mPathgray);
                            imgview2.setImageBitmap(myBitmap);

                        Log.d("Activity", "Byte array: "+ Arrays.toString(byteArray));

                    }

                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            mMap.snapshot(callback);
            Log.d("test","test2");
        }
    });

}

public byte[] bttobyte(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
    return stream.toByteArray();
}

2 个答案:

答案 0 :(得分:2)

您尝试解决的问题实际上是从字节数组中的JPEG编码中提取图像数据。它并不像在宽度和高度等于图像大小的网格中逐像素存储一样简单,正如您所暗示的那样。这是使用此

的结果
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);

例如,字节数据实际上可能编码JFIF格式:

  • 0xffd8 - SOI - 正好是1 - 图像的开始 例如0xffe0 - 零或更多 - 例如适用于JFIF的APP0 - [某种标题,JFIF或EXIF]。
  • 0xffdb - DQT - 一个或多个 - 定义量化表。这些是图像质量的主要来源。
  • 0xffcn - SOFn - 恰好一个 - 帧的开始。 SOF0表示基线DCT JPEG
  • 0xffc4 - DHT - 一个或多个 - 定义霍夫曼表。这些用于最终的无损编码步骤。
  • 0xffda - SOS - 正好一个(对于基线,如果是渐进式,多个DHT混合在一起) - 流的开始。这是实际数据开始的地方。
  • 0xffd9 - EOI - 正好是图像的一端。

基本上,一旦转换了位图,就需要JPEG解码器来解析这些数据。

我认为您想要的是使用原始位图。例如,有提取像素数据的API。如果我理解你想要正确做什么,那就是你所需要的。

Bitmap.getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)

数组pixels[]充满了数据。然后,如果要通过迭代复制数据将其存储在 w h 2D数组中,则可以解析数组。类似的东西:

int offset = 0;
int pixels2d[w][h]= new int[w][h];
for (int[] row : pixels2d) {
    System.arraycopy(pixels, offset, row, 0, row.length);
    offset += row.length;
}

答案 1 :(得分:1)

您只需创建一个包含2维的新数组

byte[][] picture = new byte[myBitmap.getWidth()][,myBitmap.getHeight()]

您可以通过

访问
byte myByte = picture[x][y]

在此之后,您逐行迭代原始的bytarray,然后按行