【问题标题】:Creating a BufferedImage from a byte array java从字节数组 java 创建 BufferedImage
【发布时间】:2012-07-05 12:02:07
【问题描述】:

我有一个字节数组,我需要将其转换为图像。我已阅读本教程:

http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/

本教程使用此导入语句中的BufferedImageImageIO

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

但我在任何地方都找不到这些课程。我有安装了 javax 的 JDK x64 版本。有人吗?

我的整个源代码:

public class ReadImageFromFileActivity extends Activity implements OnClickListener {

Context c;
Button read;
ImageView image;
byte[] fileBytes;
byte[] image1;
byte[] image2; 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    c = getApplicationContext(); 

    read = (Button)findViewById(R.id.file); 
    read.setOnClickListener(this);

    image = (ImageView)findViewById(R.id.image);

    image1 = new byte[ 500 * 500]; 
    image2 = new byte[ 500 * 500]; 

}



public static byte[] getBytesFromFile(Context c) throws IOException {

    File file = new File("/mnt/sdcard/LeftIndex.FIR");
    Uri URIpath = Uri.fromFile(file);

    InputStream is = c.getContentResolver().openInputStream(URIpath);

    long length = file.length();


    if (length > Integer.MAX_VALUE) {
        // File is too large
    }

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}


public void onClick(View v) {

    try {
        fileBytes = getBytesFromFile(c);
        convertByteArray(fileBytes); 

    } catch (IOException e) {
        Log.e("++++getBytesFromFile SAYS: ", "Couldn't read file!!!");

        e.printStackTrace();
    }

}


public void convertByteArray(byte[] buffer) {

     System.arraycopy(buffer, 64, image1, 0, 500 * 500);  
     System.arraycopy(buffer, 60, image2, 0, 500 * 500); 



     Bitmap bmp = BitmapFactory.decodeByteArray(image1, 0, image1.length); 
     ImageView image=new ImageView(this);
     image.setImageBitmap(bmp);



}

}

代码说明

在函数 getBytesFromFile 中,我从 SD 卡上的 .FIR 文件构建了一个新文件。该文件将被转换为字节数组并返回构造的字节数组。这个字节数组将被传递到一个名为convertByteArray 的函数中,该函数将被分成两个单独的字节数组。我想从这两个字节数组中的每一个构造一个位图。但是logcat只说bitmapFactory返回null。

有人对此有任何解决方案吗?

【问题讨论】:

  • 是的,没错。我有一个字节数组,我想从中创建一个图像。
  • 这 2 个导入是 JDK 的一部分。他们应该在那里。如果没有尝试重新安装JDK
  • 我不明白你想做什么?你有一个里面有图像的字节数组,你想把它转换成另一种格式,或者什么?

标签: java android swing


【解决方案1】:

Android 既没有ImageIO 也没有BufferedImage。这是另一种选择:What is the best way to serialize an image (compatible with Swing) from Java to Android?

【讨论】:

  • @TobiasMoeThorstensen 你忘了BitmapFactory.Options :)
猜你喜欢
  • 2021-04-10
  • 1970-01-01
  • 2011-07-27
  • 2016-12-10
  • 2013-08-08
  • 2011-10-05
  • 2015-11-19
  • 2013-09-23
  • 2012-04-05
相关资源
最近更新 更多