android - 字节数组到二维数组

标签 android arrays

我的代码做了什么。是拍摄 map 快照,将其转换为灰色(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();
}

最佳答案

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

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

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

  • 0xffd8 - SOI - 恰好 1 - 图像开始 例如0xffe0 - 零个或多个 - 例如APP0 for JFIF - [某种 header ,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 by h 二维数组中,则可以解析该数组。像这样的东西:

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;
}

关于android - 字节数组到二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976935/

相关文章:

c - 用 0 个元素初始化多维 C 数组

android - 在 Android WebView 中保存数据持久化

如果我使用 intent 进入主屏幕,Android 服务将被终止

android - Android上的Python 'invalid syntax'错误

java - 将 GLSurfaceView 类与 android xml 布局一起使用

javascript - 在 PHP 函数上使用 Javascript 数组,无论是在同一个文件上还是与其他函数一起在另一个文件上

c++ - 在 C++ 中将一维数组转换为二维数组

ios - 如何获取索引(:) to return multiple indices?

android - Jetpack Compose dev06 setContent() 不起作用?

c# - 将用户文本框输入(数字)放入定义的数组大小并输出?