B
bones
Gast
Hi, ich wandle ein Image zu einem Byte[] und verschicke das dann. Aber wenn ich Images rüber schicke die einen durchsichtigen Hintergrund haben, werden die Hintergründe Schwarz. Weiss einer wie ich das umgehen kann?
public static byte[] imageToByte(File inputfile)
{
try
{
BufferedImage srcImage = ImageIO.read(inputfile);
BufferedImage outImage = null;
int height = srcImage.getHeight(), width = srcImage.getWidth();
if(srcImage.getHeight() > hNew || srcImage.getWidth() > wNew)
outImage = createBufferedImage(srcImage.getScaledInstance(wNew, hNew, BufferedImage.TYPE_INT_RGB));
else
outImage = srcImage;//createBufferedImage(srcImage.getScaledInstance(width, height, BufferedImage.TYPE_INT_RGB));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if(inputfile.getName().toLowerCase().endsWith("gif")) ImageIO.write((BufferedImage)outImage, "gif", byteArrayOutputStream);
if(inputfile.getName().toLowerCase().endsWith("jpg") || inputfile.getName().toLowerCase().endsWith("jpeg"))
ImageIO.write((BufferedImage)outImage, "jpg", byteArrayOutputStream);
if(inputfile.getName().toLowerCase().endsWith("png")) ImageIO.write((BufferedImage)outImage, "png", byteArrayOutputStream);
byte[] imageData = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return imageData;
Hobbit_Im_Blutrausch hat gesagt.:Der Alpha-Kanal eines Bildes spiegelt die Transparenz einzelner Pixel wieder.
Was mir auf den ersten Blick auffällt:
1.) Beim skalieren des Bildes gibst du als hint BufferedImage.TYPE_INT_RGB an. WENN diese Angabe korrekt WÄRE, dann müsste es TYPE_INT_ARGB heißten (für die Alpha-Unterstützung). Es ist aber nicht richtig, hier gehört hin, wie skaliert werden soll (z. B. Image.SCALE_FAST oder Image.SCALE_SMOOTH)
2.) Was macht die Methode createBufferedImage?
3.) Wie wird das byte-Array weiter verarbeitet?
File theFile = files[i];
byte [] newb = Utils.imageToByte(theFile);
fis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(newb)));
theData = newb;
fis.readFully(theData);
fis.close();
long bodysize = -1;
body_file1_pre.append(""
+ "--" + boundary + "\n"
+ "Content-Disposition: form-data; name=\"datafile\"; "
+ "filename=\"" + bildfile.toString() + "\"\n"
+ "Content-Type: image/jpeg\n"
+ "\n");
cmd_post.append(""
+ "POST " + path + "?PHPSESSID=" + sessid + " HTTP/1.0\n"
+ "Content-type: multipart/form-data, boundary=" + boundary + "\n"
+ "Content-length: " + bodysize + "\n"
+ "\n");
wr.write(cmd_post.toString());
wr.write(body_fields.toString());
wr.write(body_file1_pre.toString());
wr.flush();
raw.write(theData);
raw.flush();
byte[] theData = null;
File theFile = files[i];
BufferedImage Image = ImageIO.read(theFile);
if(Image.getHeight() > Utils.hNew || Image.getWidth() > Utils.wNew)
{
theData = Utils.imageToByte(theFile);
fis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(theData)));
}
else
{
fis = new DataInputStream(new BufferedInputStream(new FileInputStream(theFile)));
theData = new byte[(int) theFile.length()];
}
public static byte[] imageToByte(File inputfile)
{
try
{
BufferedImage srcImage = ImageIO.read(inputfile);
BufferedImage outImage = null;
outImage = createBufferedImage(srcImage.getScaledInstance(wNew, hNew, BufferedImage.TYPE_INT_ARGB));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if(inputfile.getName().toLowerCase().endsWith("gif")) ImageIO.write((BufferedImage)outImage, "gif", byteArrayOutputStream);
if(inputfile.getName().toLowerCase().endsWith("jpg") || inputfile.getName().toLowerCase().endsWith("jpeg"))
ImageIO.write((BufferedImage)outImage, "jpg", byteArrayOutputStream);
if(inputfile.getName().toLowerCase().endsWith("png")) ImageIO.write((BufferedImage)outImage, "png", byteArrayOutputStream);
byte[] imageData = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return imageData;
public static BufferedImage createBufferedImage(Image image)
{
BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bi;
}
public static byte[] imageToByte(File inputfile)
{
try
{
BufferedImage srcImage = ImageIO.read(inputfile);
BufferedImage outImage = null;
outImage = createBufferedImage(srcImage.getScaledInstance(wNew, hNew, BufferedImage.SCALE_SMOOTH));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if(inputfile.getName().toLowerCase().endsWith("gif")) ImageIO.write((BufferedImage)outImage, "gif", byteArrayOutputStream);
if(inputfile.getName().toLowerCase().endsWith("jpg") || inputfile.getName().toLowerCase().endsWith("jpeg"))
ImageIO.write((BufferedImage)outImage, "jpg", byteArrayOutputStream);
if(inputfile.getName().toLowerCase().endsWith("png")) ImageIO.write((BufferedImage)outImage, "png", byteArrayOutputStream);
byte[] imageData = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return imageData;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public static BufferedImage createBufferedImage(Image image)
{
BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bi;
}