我正在尝试从服务器下载图像并将图像保存在手机上。我可以下载图像,但我需要调整图像大小,然后将图像存储到手机上。
代码是这样的。
public void requestPlatform(String strURL) throws IOException {
try {
System.out.println("requestPlatform");
byte [] imageBytes = null;
if ((im = getImage1(strURL)) != null) {
im=resizeImage(im, width, height);
if(im!=null)
{
System.out.println("Image");
}
else
System.out.println("NoImage");
imageBytes=get_Byte_Array(im);
fileCon=(FileConnection)Connector.open("file:///root1/photos/diwali1.jpg",Connector.READ_WRITE);
fileCon.create();
if(fileCon.exists())
{
System.out.println("File created");
}
else
{
System.out.println("File not created");
}
out = fileCon.openDataOutputStream();
if(imageBytes!=null)
{
out.write(imageBytes, 0, imageBytes.length);
}
if(out!=null)
{
out.close();
}
if(fileCon!=null)
{
fileCon.close();
}
im2=Image.createImage(imageBytes, 0, imageBytes.length);
canvas.repaint();
}
} catch (Exception e) {
System.err.println("Msg: " + e.toString());
}
}
public byte[] get_Byte_Array(Image img) {
int[] imgRgbData = new int[img.getWidth() * img.getHeight()];
byte[] imageData2 = null;
try
{
img.getRGB( imgRgbData, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight() );
} catch( Exception e )
{
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream( baos );
try{
for( int i = 0; i < imgRgbData.length; i++ )
{
dos.writeInt( imgRgbData[i] );
}
imageData2 = baos.toByteArray();
baos.close();
dos.close();
}catch(Exception e)
{
}
return imageData2;
}问题是,当我试图调整图像大小,然后将其保存在手机上时,它只是创建了一个文件,但没有image.So,我认为当我将图像转换为byteArray时,可能有一些错误。
发布于 2010-11-02 19:13:53
Image.createImage(Image image, int x, int y, int width, int height, int **transform**)关于省钱。您可以将数据保存到RMS (这在所有J2ME手机上都是可能的)或文件系统(仅在使用JSR-75 FileConnection API的设备上)。
发布于 2010-11-03 04:03:31
您可以使用它来调整图像的大小,但最好在服务器上调整它的大小,而不是在手机上执行此操作。您需要调整它以将图像大小调整为指定的宽度和高度。
private Image resizeImage(Image src) {
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Image tmp = Image.createImage(screenWidth, srcHeight);
Graphics g = tmp.getGraphics();
int ratio = (srcWidth << 16) / screenWidth;
int pos = ratio/2;
//Horizontal Resize
for (int x = 0; x < screenWidth; x++) {
g.setClip(x, 0, 1, srcHeight);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
Image resizedImage = Image.createImage(screenWidth, screenHeight);
g = resizedImage.getGraphics();
ratio = (srcHeight << 16) / screenHeight;
pos = ratio/2;
//Vertical resize
for (int y = 0; y < screenHeight; y++) {
g.setClip(0, y, screenWidth, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
return resizedImage;
}//resize image这段代码取自here
发布于 2014-01-28 06:01:39
这是我使用的一个很棒的图像调整脚本。希望它能满足你的需求。
public Image ResizeImage(Image image, int resizedWidth, int resizedHeight) {
int[] in = null, out = null;
int width = image.getWidth(), height = image.getHeight();
in = new int[width];
int i=0;
int dy,dx;
out = new int[resizedWidth * resizedHeight];
for (int y = 0; y < resizedHeight; y++)
{
dy = y * height / resizedHeight;
image.getRGB(in,0,width,0,dy,width,1);
for (int x = 0; x < resizedWidth; x++)
{
dx = x * width / resizedWidth;
out[(resizedWidth * y) + x] = in[dx];
}
}
Image resized = Image.createRGBImage(out,resizedWidth,resizedHeight,true);
return resized;
}https://stackoverflow.com/questions/4068680
复制相似问题