有这样一个需求:
1. Get the parameters from the HTTP request
2. Create a empty PNG image in java
3. 分開 imagedata by pixel by pixel. 每一個pixel 的數據有 RGBA.
4. 把每一個RGBA的數據轉換成Decimal
E.g DDFC33AA
R - DD (hex) to 221
G - FC (hex) to 252
B - 33(hex) to 51
A- AA(hex) to 1705. Byte []imageBytes = new byte[width*(height/no.of part)*4];
Then put the pixel information to the array
The first pixel
imageBytes [0]=A
imageBytes [1]=R
imageBytes [2]=G
Images[3]=B The second pixel
imageBytes [4]=A
imageBytes [5]=R
imageBytes [6]=G
imageBytes [7]=B The third pixel
imageBytes [8]=A
imageBytes [9]=R
imageBytes [10]=G
imageBytes [11]=B
6. Create the part image by Toolkit.getDefaultToolkit().createImage(imageBytes, 0, imageBytes.length);
7. Save the image to PNG file – filename in
我现在做到 第6步,不知道怎么往下做,有没有高手教一下,谢谢
1. Get the parameters from the HTTP request
2. Create a empty PNG image in java
3. 分開 imagedata by pixel by pixel. 每一個pixel 的數據有 RGBA.
4. 把每一個RGBA的數據轉換成Decimal
E.g DDFC33AA
R - DD (hex) to 221
G - FC (hex) to 252
B - 33(hex) to 51
A- AA(hex) to 1705. Byte []imageBytes = new byte[width*(height/no.of part)*4];
Then put the pixel information to the array
The first pixel
imageBytes [0]=A
imageBytes [1]=R
imageBytes [2]=G
Images[3]=B The second pixel
imageBytes [4]=A
imageBytes [5]=R
imageBytes [6]=G
imageBytes [7]=B The third pixel
imageBytes [8]=A
imageBytes [9]=R
imageBytes [10]=G
imageBytes [11]=B
6. Create the part image by Toolkit.getDefaultToolkit().createImage(imageBytes, 0, imageBytes.length);
7. Save the image to PNG file – filename in
我现在做到 第6步,不知道怎么往下做,有没有高手教一下,谢谢
String imageData=request.getParameter("imageData");
byte[] imageBytes=imageDataSplit(imageData,width,height,partId);//调用下面那个方法
Image image=Toolkit.getDefaultToolkit().createImage(imageBytes, 0, imageBytes.length);
public static byte[] imageDataSplit(String imageData,int width,int height,int partId){
//Split the image data
String[] image_data=imageData.split(",");
//create a byte[]
byte[]idata=new byte[width*(height/partId)*4];
for(int i=0;i<image_data.length;i++){
String r=image_data[i].substring(0,2);
String g=image_data[i].substring(2,4);
String b=image_data[i].substring(4,6);
String a=image_data[i].substring(6,8);
//change to Decimal
Integer ri=new Integer(Integer.parseInt(r,16));
Integer gi=new Integer(Integer.parseInt(g,16));
Integer bi=new Integer(Integer.parseInt(b,16));
Integer ai=new Integer(Integer.parseInt(a,16));
idata[i*4]=ai.byteValue();
idata[i*4+1]=ri.byteValue();
idata[i*4+2]=gi.byteValue();
idata[i*4+3]=bi.byteValue();
}
return idata;
}