-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic image manipulation (?resize, ?format) on images
- Loading branch information
Showing
5 changed files
with
277 additions
and
78 deletions.
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
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
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,46 @@ | ||
/* | ||
* 🪶 hazel: Minimal, simple, and open source content delivery network made in Kotlin | ||
* Copyright 2022 Noel <[email protected]> | ||
* | ||
* 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 | ||
* | ||
* http://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 dev.floofy.hazel.core | ||
|
||
import java.awt.Image | ||
import java.awt.image.BufferedImage | ||
import java.io.ByteArrayInputStream | ||
import java.io.ByteArrayOutputStream | ||
import javax.imageio.ImageIO | ||
|
||
object ImageManipulator { | ||
fun resize(stream: ByteArrayInputStream, format: String, width: Int, height: Int): ByteArray { | ||
val buffImage = ImageIO.read(stream) | ||
val resulting = buffImage.getScaledInstance(width, height, Image.SCALE_DEFAULT) | ||
val output = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) | ||
output.graphics.drawImage(resulting, 0, 0, null) | ||
|
||
val baos = ByteArrayOutputStream() | ||
ImageIO.write(output, format, baos) | ||
|
||
return baos.toByteArray() | ||
} | ||
|
||
fun format(stream: ByteArrayInputStream, to: String): ByteArray { | ||
val bufferedImage = ImageIO.read(stream) | ||
val out = ByteArrayOutputStream() | ||
|
||
ImageIO.write(bufferedImage, to, out) | ||
return out.toByteArray() | ||
} | ||
} |
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
Oops, something went wrong.