歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android圖像處理簡介の圖像存儲和元數據

Android圖像處理簡介の圖像存儲和元數據

日期:2017/3/1 10:33:14   编辑:Linux編程
Android提供Content Provider來實現應用程序之間的數據共享,provider提供了標准的接口用於存儲和檢索多種類型的數據。圖像 、音頻和視頻的標准content provider就是MediaStore。

1)獲取圖像的URI

要獲得標准的圖像存儲路徑,我們需要獲得MediaStore的引用,而這是通過content resolver來實現的(因為使用Content resolver可以獲取content provider,而MediaStore就是一個content provider)。

傳遞指定的URI給content resolver,可以得到對應的content provider,由於是新增一張圖像,所以使用insert方法,相應的URI是android.provider.MediaStore.Images.Media類定義的常量EXTERNAL_CONTENT_URI。這個常量說明我們要將圖像存儲到主外部存儲器中,通常就是SD卡;如果要將圖像存儲到設備內存中,則使用INTERNAL_CONTENT_URI。當然對於媒體文件的存儲而言,由於尺寸一般都比較大,因此會優先考慮使用EXTERNAL_CONTENT_URI。

Content resolver類的insert函數返回值是URI類型:

[java]
  1. Uri imageFileUri = getContentResolver().insert(
  2. Media.EXTERNAL_CONTENT_URI, new ContentValues());
  3. // Start the Camera App
  4. Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  5. it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
  6. startActivityForResult(it, CAMERA_RESULT);

上面代碼中的ContentValues對象是捕獲的圖像在創建時要關聯的元數據,當然,上面的元數據是空的。我們可以使用put函數將元數據信息寫入ContentValues中,ContentValues是以鍵值對的形式存儲數據的,鍵名是定義在android.provider.MediaStore.Images.Media類中的常量:

[java]
  1. // Save the name and description of an image in a ContentValues map
  2. ContentValues contentValues = new ContentValues(3);
  3. contentValues.put(Media.DISPLAY_NAME, "ASCE1885_TITLE");
  4. contentValues.put(Media.DESCRIPTION, "ASCE1885_DESCRIPTION");
  5. contentValues.put(Media.MIME_TYPE, "image/jpeg");
  6. // Add a new recode without the bitmap, but with some values set.
  7. // insert() returns the URI of the new record
  8. Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);

上面獲取的Uri可能類似於:

content://media/external/images/media/16

這裡說明一點,以content開頭的Uri一般都是被content provider使用的,例如上面的Uri是被MediaStore使用的一樣。

反過來根據Uri,我們可以用來檢索這個Uri對應路徑中的圖像數據,代碼如下:

[java]
  1. Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null,bmpFactory);
  2. <p> </p>

在我們捕獲圖像並存放在MediaStore中後,如果還想再增加元數據信息,那麼可以使用ContentResolver的update函數來實現:

[java]
  1. // Update the MediaStore record with Title and Description
  2. ContentValues contentValues = new ContentValues(3);
  3. contentValues.put(Media.DISPLAY_NAME, "WEN1885_TITLE");
  4. contentValues.put(Media.DESCRIPTION, "WEN1885_DESCRIPTION");
  5. getContentResolver().update(imageFileUri, contentValues, null, null);

完整的代碼例子如下,先看layout/main.xml文件:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ImageView
  8. android:id="@+id/ReturnedImageView"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"/>
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="Title:"
  15. android:id="@+id/TitleTextView" />
  16. <EditText
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/TitleEditText"/>
  20. <TextView
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="Description"
  24. android:id="@+id/DescriptionTextView"/>
  25. <EditText
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:id="@+id/DescriptionEditText"/>
  29. <Button
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:id="@+id/TakePictureButton"
  33. android:text="Take Picture"/>
  34. <Button
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:id="@+id/SaveDataButton"
  38. android:text="Save Data"/>
  39. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved