I am reading two images in a servlet and need to show both at the same time.
Currently, only one image is shown (one that is written first). unable to write another image.
I do not get any error.
My servlet code goes like this :
BufferedImage buffImageA = ImageIO.read(getServletContext().getResourceAsStream("/images/3520276097315A.jpg"));
BufferedImage buffImageB = ImageIO.read(getServletContext().getResourceAsStream("/images/3520276097315B.jpg"));
logger.logDebug("Images has been read");
watermark(buffImageA,ApplicationConfig.WATERMARK_TEXT);
watermark(buffImageB,ApplicationConfig.WATERMARK_TEXT);
byte[] resultDataA = encodeJPEG(buffImageA, 100);
byte[] resultDataB = encodeJPEG(buffImageB, 100);
byte[] combinedImage = new byte[resultDataA.length+resultDataB.length];
for(int i=0; i<resultDataA.length ;i++){
combinedImage[i] = resultDataA[i];
}
for(int i=resultDataA.length; i<resultDataB.length ;i++){
combinedImage[i] = resultDataB[i];
}
response.setContentType("image/jpeg");
response.setContentLength(resultDataA.length + resultDataB.length);
OutputStream os = response.getOutputStream();
os.write(combinedImage);
os.close();
//Watermarking process goes here
private void watermark(BufferedImage original, String watermarkText) {
}
private byte[] encodeJPEG(BufferedImage image, int quality) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream((int) ((float) image.getWidth() * image.getHeight() / 4));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(image);
byte[] result = baos.toByteArray();
baos.close();
return result;
}
I have tried using ImageIO.write to write the image but failed to get what is desired.
Your second for loop must be like this:
for(int i=resultDataA.length; i<resultDataB.length+resultDataA.length ;i++){
combinedImage[i] = resultDataB[i-resultDataA.length];
}
EDIT :
This is a compilable, runnable example close to what you're expecting :
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import java.awt.Graphics;
public class Essai2 {
public static void main(String[] args) {
try {
byte[] imageInByte;
BufferedImage originalImage1 = ImageIO.read(new File("essai1.jpg"));
BufferedImage originalImage2 = ImageIO.read(new File("essai2.jpg"));
// convert BufferedImage to byte array
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
ImageIO.write(originalImage1, "jpg", baos1);
ImageIO.write(originalImage2, "jpg", baos2);
baos1.flush();
baos2.flush();
byte[] ba1 = baos1.toByteArray();
byte[] ba2 = baos2.toByteArray();
imageInByte = new byte[ba1.length + ba2.length];
//System.out.println(new String(imageInByte));
System.arraycopy(ba1, 0, imageInByte, 0, ba1.length);
//System.out.println(new String(imageInByte));
System.arraycopy(ba2, 0, imageInByte, ba1.length, ba2.length);
//System.out.println(new String(imageInByte));
baos1.close();
baos2.close();
// convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
int w = Math.max(originalImage1.getWidth(), originalImage2.getWidth());
//int h = Math.max(originalImage1.getHeight(), originalImage2.getHeight());
int h = originalImage1.getHeight() + originalImage2.getHeight();
BufferedImage bImageFromConvert = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
//BufferedImage bImageFromConvert = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR );
//BufferedImage bImageFromConvert = ImageIO.read(in);
Graphics g = bImageFromConvert.getGraphics();
g.drawImage(originalImage1, 0, 0, null);
g.drawImage(originalImage2, 0, originalImage1.getHeight(), null);
ImageIO.write(bImageFromConvert, "jpg", new File("result.jpg"));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
essai1.jpg:
essai2.jpg:
result.jpg:
I haven't found for the moment why there's a third color added to result.jpg. But I think this example can help you and I will fix my code asap.
EDIT2 :
Change :
BufferedImage bImageFromConvert = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
To :
BufferedImage bImageFromConvert = new BufferedImage(w, h, originalImage1.getType());
And it will works fine.
result.jpg:
Related
I am developing application for recognising Sudoku from paper and showing it on the screen. Since I am using OpenCV, I've tried to convert Mat object to PNG image. I tried to use this code:
private static void Mat2BufferedImage(Mat m, String s){
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b);
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
File f = new File("resources/img-" + s + ".png");
try {
ImageIO.write(image, "PNG", f);
} catch (IOException e) {
e.printStackTrace();
}
}
, but I am unable since BufferedImage is not supported anymore. Is there any other way to do this? I am not so good with OpenCV.
My error when sending an image Client-Server-Client is "Corrupt JPEG data: 4350674 extraneous bytes before marker 0xd9" What I can change? Here is my code :
SERVER
InputStream is = klienciRemoteScreen[1].getInputStream();
OutputStream os = klienciRemoteScreen[0].getOutputStream();
byte[] bytesize = new byte[4];
is.read(bytesize);
int size = ByteBuffer.wrap(bytesize).asIntBuffer().get();
byte [] image= new byte [size];
byte[]bufferimage = new byte [1];
os.write(bytesize);
os.flush();
int count;
while((count = is.read(image)) > 0)
{
os.write(image);
os.flush();
}
os.close();
CLIENT sending image to server
OutputStream os = noguiklient.sockRemoteScreen.getOutputStream();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(dim);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rectangle);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size =
ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
int sizeas = ByteBuffer.wrap(size).asIntBuffer().get();
os.write(size);
os.write(byteArrayOutputStream.toByteArray());
os.close();
CLIENT Receiving from server
JFrame okno = new JFrame();
okno.setSize(1300, 1000);
okno.setTitle(" Remote screen");
okno.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setSize(1300,1000);
panel.setLayout(null);
okno.add(panel);
okno.setVisible(true);
InputStream is = Klient.sockRemoteScreen.getInputStream();
File photo = new File ("photo") ;
FileOutputStream fos = new FileOutputStream(photo);
byte[] buffer = new byte[4];
is.read(buffer);
int size = ByteBuffer.wrap(buffer).asIntBuffer().get();
byte[] image = new byte [size];
int count;
int i =0 ;
while((count = is.read(image)) > 0)
{
fos.write(image);
fos.flush();
}
ImageIcon screen = new ImageIcon("photo");
Image imagefinal = screen.getImage();
imagefinal =
imagefinal.getScaledInstance(okno.getWidth(),okno.getHeight(), Image.SCALE_SMOOTH);
BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics graphics = panel.getGraphics();
while(true)
{
graphics.drawImage(imagefinal, 0, 0, panel.getWidth(), panel.getHeight(), panel);
}
Example of this bad Image
When I try to send with ImageIO everything is fine but I want to try with bytes and there is a problem.
Thank you :)
I wanted to read a grayscale image values into a 2d array, change the values of the 2d array and create a new grayscale image from the modified values of 2d array.
Following is the code to read the grayscale image into 2D array(arr[][]).
package dct;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.io.File;
import javax.imageio.ImageIO;
/**
*
* #author jain
*/
public class writeGR {
public static void main(String[] args)
{
File file = new File("lightning.jpg");
BufferedImage img = null;
try
{
img = ImageIO.read(file);
}
catch(Exception e)
{
e.printStackTrace();
}
int width = img.getWidth();
int height = img.getHeight();
int[][] arr = new int[width][height];
Raster raster = img.getData();
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
arr[i][j] = raster.getSample(i, j, 0);
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
image.setData(raster);
//
//BufferedImage image1 = image;
try
{
File ouptut = new File("grayscale.png");
ImageIO.write(image, "png", ouptut);
}
catch(Exception e)
{
e.printStackTrace();
}
}// main
}
Now I want to change the "arr[][]" vlaues and create a new grayscale image out of that. How to do this?
The above problem can be solved by the following code:
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
{
tempArr[0] = 100;
raster1.setPixel(i, j, tempArr);
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
image.setData(raster1);
try
{
File ouptut = new File("change.png");
ImageIO.write(image, "png", ouptut);
}
catch(Exception e)
{
e.printStackTrace();
}
After loading the image, I want to create an exact copy of the image whereby the quality and scale can remain the same. With my current code, the quality was reduced.
public class Image {
private static final String path = "C:/Users.../src/7horses.jpg";
private static final File file = new File(path);
static BufferedImage deepCopy(BufferedImage bi) throws IOException {
String saveAs = "copy.jpg";
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
BufferedImage cImg = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
File saveImage = new File("C:/Users.../src", saveAs);
ImageIO.write(cImg, "jpg", saveImage);
return cImg;
}
public static void main(String[] args) throws IOException {
BufferedImage cp, img;
img = ImageIO.read(file);
cp = deepCopy(img);
}
}
try just to copy your image file, use this code :
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(new File("path/to/img/src"));
os = new FileOutputStream(new File("path/to/img/dest"));
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
if you are using Java 8 then you can just call Files.copy method, check it in the docs
Go from using:
ImageIO.write(cImg, "jpg", saveImage);
To the following: (Don't have my dev environment here to test this but think I have this close for you.)
Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iterator.next();
ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(1); //0 is max Compression 1 is max quality
FileImageOutputStream output = new FileImageOutputStream(saveImage);
writer.setOutput(output);
IIOImage image = new IIOImage(cImg, null, null);
writer.write(null, image, iwp);
writer.dispose();
output.close();
I have this code where i get an InputStream and create an image:
Part file;
// more code
try {
InputStream is = file.getInputStream();
File f = new File("C:\\ImagenesAlmacen\\QR\\olaKeAse.jpg");
OutputStream os = new FileOutputStream(f);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
} catch (IOException e) {
System.out.println("Error");
}
The problem is that I have to resize that image before i create if from the InputStream
So how to resize what I get from the InputStream and then create that resized image. I want to set the largest side of the image to 180px and resize the other side with that proportion.
Example:
Image = 289px * 206px
Resized image = 180px* 128px
I did this:
try {
InputStream is = file.getInputStream();
Image image = ImageIO.read(is);
BufferedImage bi = this.createResizedCopy(image, 180, 180, true);
ImageIO.write(bi, "jpg", new File("C:\\ImagenesAlmacen\\QR\\olaKeAse.jpg"));
} catch (IOException e) {
System.out.println("Error");
}
BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight, boolean preserveAlpha) {
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
Graphics2D g = scaledBI.createGraphics();
if (preserveAlpha) {
g.setComposite(AlphaComposite.Src);
}
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
return scaledBI;
}
And I did not use the other code.
Hope helps someone!