简体   繁体   English

从字节数组Java创建BufferedImage

[英]Creating a BufferedImage from a byte array java

I have a byte array and I need to convert this to a image. 我有一个字节数组,我需要将其转换为图像。 I've read this tutorial: 我已经阅读了本教程:

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

This tutorial uses the BufferedImage and ImageIO which is in this import statement 本教程使用此import语句中的BufferedImageImageIO

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

But I can't find these classes anywhere. 但是我在任何地方都找不到这些类。 I have the JDK x64 version with the javax installed. 我有安装了Javax的JDK x64版本。 Anybody? 任何人?

My whole source code: 我的整个源代码:

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);



}

} }

Explanation of the code 代码说明

in the function getBytesFromFile I construct a new File out of a .FIR file at my SD-card. 在函数getBytesFromFile我在SD卡上使用.FIR文件构造了一个新文件。 This file will be converted to a Byte array and returns the constructed byte array. 该文件将转换为Byte数组,并返回构造的byte数组。 This byte-array will be passed into a function called convertByteArray which will be splitted into two separate Byte arrays. 该字节数组将传递到一个名为convertByteArray的函数中,该函数将convertByteArray分为两个单独的Byte数组。 From each of these two byte arrays I want to construct a Bitmap. 从这两个字节数组的每一个,我想构造一个位图。 But logcat only says that the bitmapFactory returns null. 但是logcat只说bitmapFactory返回null。

Anybody have any solution to this? 有人对此有解决方案吗?

Android does not have neither ImageIO nor BufferedImage . Android既没有ImageIO也没有BufferedImage Here is an alternative: What is the best way to serialize an image (compatible with Swing) from Java to Android? 这是一个替代方案: 将图像(与Swing兼容)从Java序列化到Android的最佳方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM