将java.awt.*代码转换为Android.graphics.*代码
我正在尝试移植一些使用将java.awt.*代码转换为Android.graphics.*代码,java,android,bitmap,awt,android-bitmap,Java,Android,Bitmap,Awt,Android Bitmap,我正在尝试移植一些使用java.awt.*library编写的图形代码,以替代使用android.graphics.*library。然而,我在图形方面没有太多经验 下面是java.awt.*代码(有效): 这是MyImageBitmap类: /** * A <code>MyImageBitmap</code> instance contains an image information in bitmap format. */ public class MyImag
java.awt.*
library编写的图形代码,以替代使用android.graphics.*
library。然而,我在图形方面没有太多经验
下面是java.awt.*
代码(有效):
这是MyImageBitmap类:
/**
* A <code>MyImageBitmap</code> instance contains an image information in bitmap format.
*/
public class MyImageBitmap implements Serializable {
//...member variables
/**
* Creates an instance of <code>MyImageBitmap</code> with specified data.
*
* @param pixels the image pixes, not null
* @param width the image width, not null
* @param height the image height, not null
* @param ppi pixel per inch, not null
* @param depth the image depth, not null
* @param lossyFlag lossy flag, not null
*/
public MyImageBitmap(byte[] pixels, int width, int height, int ppi, int depth, int lossyFlag) {
this.pixels = pixels;
this.width = width;
this.height = height;
this.ppi = ppi;
this.depth = depth;
this.lossyFlag = lossyFlag;
this.length = pixels != null ? pixels.length : 0;
}
//...getters
}
这是我尝试过的(没有成功):
private byte[]转换(MyImageBitmap MyImageBitmap,字符串格式){
int width=myImageBitmap.getWidth();
int height=myImageBitmap.getHeight();
字节[]imgRGB888=myImageBitmap.getPixels();
位图bmp2=Bitmap.createBitmap(宽度、高度、Bitmap.Config.ARGB_8888);
int[]颜色=新int[宽度*高度];
int r,g,b;
对于(int ci=0;ci
当我运行上面的代码时(我尝试在awt代码上进行移植),我在这一行上得到了一个
ArrayIndexOutOfBoundsException
,r=(int)(0xFF&imgRGB888[3*ci])代码>我最终弄明白了问题所在。我的转换算法
字节数组到int颜色数组错误。下面是正确的实现。图像现在已正确显示在Android图像视图中
/**
* Converts the given <code>MyImageBitmap</code> to the specified image format and returns it as byte array.
*
* @param myImageBitmap the given bitmap, not null
* @param format the given target image format, not null
* @return the converted data in byte array format, not null
*/
private byte[] convert(MyImageBitmap myImageBitmap, String format) {
int width = myImageBitmap.getWidth();
int height = myImageBitmap.getHeight();
byte[] imgRGB888 = myImageBitmap.getPixels();
Bitmap bmp2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] colors = new int[width * height];
//We need to convert the image from a byte array to a
// int color array so we can create the Android Bitmap
int r,g,b;
for (int ci = 0; ci < colors.length; ci++) {
r = (int)(0x000000ff & imgRGB888[ci]);
g = (int)(0x000000ff & imgRGB888[ci]);
b = (int)(0x000000ff & imgRGB888[ci]);
colors[ci] = Color.rgb(r, g, b);
}
bmp2.setPixels(colors, 0, width, 0, 0, width, height);
Bitmap.CompressFormat compressFormat;
if (format.equals("jpeg")){
compressFormat = android.graphics.Bitmap.CompressFormat.JPEG;
}else{//must be png
compressFormat = android.graphics.Bitmap.CompressFormat.PNG;
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){
bmp2.compress(compressFormat, 100, outputStream);
return outputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
我最终弄明白了问题所在。我的转换算法
字节数组到int颜色数组错误。下面是正确的实现。图像现在已正确显示在Android图像视图中
/**
* Converts the given <code>MyImageBitmap</code> to the specified image format and returns it as byte array.
*
* @param myImageBitmap the given bitmap, not null
* @param format the given target image format, not null
* @return the converted data in byte array format, not null
*/
private byte[] convert(MyImageBitmap myImageBitmap, String format) {
int width = myImageBitmap.getWidth();
int height = myImageBitmap.getHeight();
byte[] imgRGB888 = myImageBitmap.getPixels();
Bitmap bmp2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] colors = new int[width * height];
//We need to convert the image from a byte array to a
// int color array so we can create the Android Bitmap
int r,g,b;
for (int ci = 0; ci < colors.length; ci++) {
r = (int)(0x000000ff & imgRGB888[ci]);
g = (int)(0x000000ff & imgRGB888[ci]);
b = (int)(0x000000ff & imgRGB888[ci]);
colors[ci] = Color.rgb(r, g, b);
}
bmp2.setPixels(colors, 0, width, 0, 0, width, height);
Bitmap.CompressFormat compressFormat;
if (format.equals("jpeg")){
compressFormat = android.graphics.Bitmap.CompressFormat.JPEG;
}else{//must be png
compressFormat = android.graphics.Bitmap.CompressFormat.PNG;
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){
bmp2.compress(compressFormat, 100, outputStream);
return outputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Converts the given <code>MyImageBitmap</code> to the specified image format and returns it as byte array.
*
* @param myImageBitmap the given bitmap, not null
* @param format the given target image format, not null
* @return the converted data in byte array format, not null
*/
private byte[] convert(MyImageBitmap myImageBitmap, String format) {
int width = myImageBitmap.getWidth();
int height = myImageBitmap.getHeight();
byte[] imgRGB888 = myImageBitmap.getPixels();
Bitmap bmp2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] colors = new int[width * height];
//We need to convert the image from a byte array to a
// int color array so we can create the Android Bitmap
int r,g,b;
for (int ci = 0; ci < colors.length; ci++) {
r = (int)(0x000000ff & imgRGB888[ci]);
g = (int)(0x000000ff & imgRGB888[ci]);
b = (int)(0x000000ff & imgRGB888[ci]);
colors[ci] = Color.rgb(r, g, b);
}
bmp2.setPixels(colors, 0, width, 0, 0, width, height);
Bitmap.CompressFormat compressFormat;
if (format.equals("jpeg")){
compressFormat = android.graphics.Bitmap.CompressFormat.JPEG;
}else{//must be png
compressFormat = android.graphics.Bitmap.CompressFormat.PNG;
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){
bmp2.compress(compressFormat, 100, outputStream);
return outputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}