android for opencv (2) byte[] ,Bitmap 与 Mat 类型转换,Bitmap保存照片
16lz
2021-01-25
(一) byte[] 转换为Mat,Mat 转 byte[]
public Mat byteAndMat(Mat image) { int width = image.cols(); int height = image.rows(); int dims = image.channels(); byte[] data = new byte[width*height*dims]; image.get(0, 0, data); //Mat转byte int index = 0; int r=0, g=0, b=0; for(int row=0; row
(二)byte[] 转换为Bitmap ,Bitmap 转 byte[]
//Bitmap转bytepublic static byte[] bitmapToByteArray(Bitmap image) {//calculate how many bytes the image consists of.int bytes = bm.getByteCount();ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new bufferimage.copyPixelsToBuffer(buffer); //Move the byte data to the bufferreturn buffer.array(); //Get the underlying array containing the data.}//或者是下面的这种形式public void BitmapToBYTE(Bitmap image) { int bytes = image.getByteCount(); ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a buffer image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer byte[] temp = buffer.array(); // Get the underlying array }
//Byte转Bitmappublic Bitmap ByteArray2Bitmap(byte[] data, int width, int height) { int Size = width * height; int[] rgba = new int[Size]; for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) { rgba[i * width + j] = 0xff000000; } Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bmp.setPixels(rgba, 0 , width, 0, 0, width, height); return bmp;}
(三)Mat与Bitmap类型转换
//Mat转Bitmappublic static Bitmap matToBitmap(Mat mat) {Bitmap resultBitmap = null;if (mat != null) {resultBitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);if (resultBitmap != null)Utils.matToBitmap(mat, resultBitmap);}return resultBitmap;}//Bitmap转Matpublic static Mat bitmapToMat(Bitmap bm) {Bitmap bmp32 = bm.copy(Bitmap.Config.RGB_565, true);Mat imgMat = new Mat ( bm.getHeight(), bm.getWidth(), CvType.CV_8UC2, new Scalar(0));Utils.bitmapToMat(bmp32, imgMat);return imgMat;}
(四)Bitmap保存照片jpg格式为例
public int saveImageToGallery(Bitmap bmp) { //生成路径 String root = Environment.getExternalStorageDirectory().getAbsolutePath(); String dirName = "saveimage"; File appDir = new File(root , dirName); if (!appDir.exists()) { appDir.mkdirs(); } String fileName = "bitmap.jpg"; //获取文件 File file = new File(appDir, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); return 0; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return -1; }
(五)读写权限动态获取
Android6.0以后,读写内存、操作图像等除了需要在AndroidManifest.xml文件中添加相关权限:
另外还需要在Activity中动态获取,代码如下:
private void checkPermission() { // Storage Permissions final int REQUEST_EXTERNAL_STORAGE = 1; String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; try { //检测是否有写的权限 int permission = ActivityCompat.checkSelfPermission(MainActivity.this, "android.permission.WRITE_EXTERNAL_STORAGE"); if (permission != PackageManager.PERMISSION_GRANTED) { // 如果没有读写权限,申请读写权限 ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE); } } catch (Exception e) { e.printStackTrace(); } }
将上述代码添加到onCreate中即可,Activity运行时会弹出读写权限提示框。
参考:
https://blog.csdn.net/qluojieq/article/details/78289795?locationNum=7&fps=1
https://blog.csdn.net/u013547134/article/details/40918513
https://www.jb51.net/article/137657.htm
https://github.com/fiubaar/cliente/blob/3466e5e35a8459aef7174cf6b64a2ce81916656a/src/com/fi/uba/ar/utils/MatUtils.java
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Android(安卓)ethernet support
- Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法
- Android(安卓)adb shell 命令大全
- MTK android 6572的一个编译的脚本