-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6209a9d
commit 5d3c7c4
Showing
7 changed files
with
109 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
package com.mercurys.unfinished; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.*; | ||
import java.nio.ByteBuffer; | ||
import java.nio.file.*; | ||
import java.text.*; | ||
import java.util.Calendar; | ||
|
||
public class MImageHandler { | ||
|
||
DataOutputStream outputStream; | ||
DataInputStream inputStream; | ||
String imageDownloadPath; | ||
|
||
public MImageHandler(DataOutputStream outputStream) { | ||
this.outputStream = outputStream; | ||
} | ||
|
||
public MImageHandler(DataInputStream inputStream) { | ||
this.inputStream = inputStream; | ||
} | ||
|
||
public void sendImageFileAsByteStream(final String imageFileName) throws IOException { | ||
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
final BufferedImage bufferedImage = ImageIO.read(new File(imageFileName)); | ||
ImageIO.write(bufferedImage, "png", byteArrayOutputStream); | ||
|
||
final byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array(); | ||
|
||
this.outputStream.write(size); | ||
this.outputStream.write(byteArrayOutputStream.toByteArray()); | ||
} | ||
|
||
public BufferedImage getImageAsBufferedImage() { | ||
try { | ||
final byte[] imgSize = new byte[4]; | ||
this.inputStream.readFully(imgSize); | ||
final byte[] imgArr = new byte[ByteBuffer.wrap(imgSize).asIntBuffer().get()]; | ||
this.inputStream.readFully(imgArr); | ||
return ImageIO.read(new ByteArrayInputStream(imgArr)); | ||
|
||
} catch (final IOException e) { | ||
System.out.println("Exception Occurred!"); | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
public void downloadBufferedImage(final BufferedImage image) { | ||
if (image == null) { | ||
return; | ||
} | ||
final String s = System.getProperty("file.separator"); | ||
final String downloadPath = | ||
MessageFormat.format("{0}{1}Pictures{1}Mercurys{1}{3}{1}ReceivedImage{2}.png", | ||
System.getProperty("user.home"), s, new SimpleDateFormat("HHmmss") | ||
.format(Calendar.getInstance().getTime()), new SimpleDateFormat("dd.MM.yyyy") | ||
.format(Calendar.getInstance().getTime())); | ||
try { | ||
Files.createDirectories(Paths.get(downloadPath.substring(0, downloadPath.lastIndexOf(s)) + s)); | ||
ImageIO.write(image, "png", new File(downloadPath)); | ||
this.imageDownloadPath = downloadPath; | ||
} catch (final IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public String getImageDownloadPath() { | ||
return imageDownloadPath; | ||
} | ||
} |
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