-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from android/ultrahdr_compression
[Graphics] [Sample] UltraHDR Image compression
- Loading branch information
Showing
5 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+6.6 MB
samples/graphics/ultrahdr/src/main/assets/ultrahdr/ultrahdr_cityscape.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
159 changes: 159 additions & 0 deletions
159
...src/main/java/com/example/platform/graphics/ultrahdr/display/CompressingUltraHDRImages.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.platform.graphics.ultrahdr.display | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.annotation.RequiresApi | ||
import androidx.fragment.app.Fragment | ||
import com.example.platform.graphics.ultrahdr.databinding.CompressUltrahdrBinding | ||
import com.google.android.catalog.framework.annotations.Sample | ||
import java.io.ByteArrayOutputStream | ||
|
||
|
||
@RequiresApi(34) | ||
@Sample( | ||
name = "Compressing UltraHDR Images", | ||
description = "This sample demonstrates displaying an UltraHDR image in a Compose View and an Android View", | ||
documentation = "https://developer.android.com/guide/topics/media/hdr-image-format", | ||
tags = ["UltraHDR"], | ||
) | ||
|
||
class CompressingUltraHDRImages : Fragment() { | ||
/** | ||
* Android ViewBinding. | ||
*/ | ||
private var _binding: CompressUltrahdrBinding? = null | ||
private val binding get() = _binding!! | ||
var orignalImageSize: Long = 0 | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
_binding = CompressUltrahdrBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
// The ColorModeControls Class contain the necessary function to change the activities | ||
// ColorMode to HDR, which allows and UltraHDRs images gain map to be used to enhance the | ||
// image. | ||
binding.colorModeControls.setWindow(requireActivity().window) | ||
|
||
// Setup editing options and on listeners | ||
initializedUltraHDRImageButtonListeners() | ||
|
||
// Set original ultra hdr image | ||
binding.optionOrignalUltrahdr.isChecked = true | ||
binding.optionOrignalUltrahdr.callOnClick() | ||
} | ||
|
||
private fun initializedUltraHDRImageButtonListeners() { | ||
|
||
//Select the edit operation : crop, rotate, scale | ||
binding.optionCompressedImage.setOnClickListener { | ||
compressAndDisplayUltraHDRImage() | ||
} | ||
|
||
//Select the edit operation : crop, rotate, scale | ||
binding.optionOrignalUltrahdr.setOnClickListener { | ||
displayOriginalUltraHDRImage() | ||
} | ||
} | ||
|
||
/** | ||
* Updated the currently displayed UltraHDR ORIGINAL image. | ||
*/ | ||
private fun displayOriginalUltraHDRImage() { | ||
|
||
val stream = context?.assets?.open(ULTRA_HDR_IMAGE) | ||
val bitmap = BitmapFactory.decodeStream(stream) | ||
binding.imageContainer.setImageBitmap(bitmap) | ||
|
||
// display original size of image | ||
val outputStream = ByteArrayOutputStream() | ||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) | ||
val outputStreamSize = outputStream.toByteArray().size.toLong() | ||
|
||
// convert from bytes to MB for displaying text | ||
orignalImageSize = (outputStreamSize / (1024 * 1024)) | ||
|
||
" Displayed Original Image size : $orignalImageSize MB".also { | ||
binding.ultrahdrImageSizeText.text = it | ||
} | ||
} | ||
|
||
/** | ||
* Updated the currently displayed UltraHDR ORIGINAL image. | ||
*/ | ||
private fun compressAndDisplayUltraHDRImage() { | ||
|
||
//open original image | ||
val stream = context?.assets?.open(ULTRA_HDR_IMAGE) | ||
val bitmap = BitmapFactory.decodeStream(stream) | ||
|
||
//Compress ultra-hdr image | ||
val outputStream = ByteArrayOutputStream() | ||
bitmap.compress( | ||
Bitmap.CompressFormat.JPEG, | ||
ULTRA_HDR_IMAGE_COMPRESSION_QUALITY, | ||
outputStream, | ||
) | ||
|
||
|
||
// Display size of compressed image. For demo purposes only | ||
val imageInByte: ByteArray = outputStream.toByteArray() | ||
//convert fom bytes to megabytes | ||
val compressedImageSizeInMegabytes: Long = imageInByte.size.toLong() / (1024 * 1024) | ||
|
||
// Convert compressed stream into a bitmap | ||
val compressedByteArray: ByteArray = imageInByte | ||
val compressedBitmap = | ||
BitmapFactory | ||
.decodeByteArray(compressedByteArray, 0, compressedByteArray.size) | ||
|
||
|
||
//Load the compressed bitmap | ||
binding.imageContainer.setImageBitmap(compressedBitmap) | ||
|
||
//Set compressed image details as text for informational purposes | ||
("Compressed Image Quality : ${ULTRA_HDR_IMAGE_COMPRESSION_QUALITY}% \n Displayed Compressed Image Size : ${compressedImageSizeInMegabytes} MB ").also { | ||
binding.ultrahdrImageSizeText.text = it | ||
} | ||
} | ||
|
||
companion object { | ||
/** | ||
* Sample UltraHDR images paths | ||
*/ | ||
private const val ULTRA_HDR_IMAGE = "ultrahdr/ultrahdr_cityscape.jpg" | ||
|
||
//Image compression quality from 0-100. Refer: https://developer.android.com/reference/android/graphics/Bitmap.CompressFormat | ||
private const val ULTRA_HDR_IMAGE_COMPRESSION_QUALITY = 40 | ||
} | ||
} | ||
|
||
|
||
|
69 changes: 69 additions & 0 deletions
69
samples/graphics/ultrahdr/src/main/res/layout/compress_ultrahdr.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?xml version="1.0" encoding="utf-8"?><!-- | ||
~ Copyright 2023 The Android Open Source Project | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ https://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<com.example.platform.graphics.ultrahdr.common.ColorModeControls | ||
android:id="@+id/color_mode_controls" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
|
||
<RadioGroup | ||
android:id="@+id/image_operation" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="@dimen/ultrahdr_color_mode_current_mode_padding" | ||
android:orientation="horizontal"> | ||
|
||
<RadioButton | ||
android:id="@+id/option_orignal_ultrahdr" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:checked="false" | ||
android:text="@string/displaying_orignal_ultrahdr" /> | ||
|
||
<RadioButton | ||
android:id="@+id/option_compressed_image" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:checked="false" | ||
android:text="@string/displaying_compressed_image" /> | ||
</RadioGroup> | ||
|
||
<TextView | ||
android:id="@+id/ultrahdr_image_size_text" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:gravity="center_horizontal" | ||
android:lines="3" | ||
android:padding="@dimen/ultrahdr_color_mode_current_mode_padding" | ||
android:text="@string/ultra_hdr_image_size" | ||
tools:text="@string/ultra_hdr_image_size" /> | ||
|
||
|
||
<ImageView | ||
android:id="@+id/image_container" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:contentDescription="@null" /> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters