バイト配列があり、これを画像に変換する必要があります。私はこのチュートリアルを読みました:
http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/
このチュートリアルでは、この import ステートメントにあるBufferedImage
andを使用しますImageIO
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 ファイルから新しいファイルを作成します。このファイルは Byte 配列に変換され、構築されたバイト配列を返します。このバイト配列は、呼び出された関数に渡され、convertByteArray
2 つの別個のバイト配列に分割されます。これらの 2 つのバイト配列のそれぞれから、ビットマップを構築したいと考えています。しかし、logcat は、bitmapFactory が null を返すとだけ言っています。
誰にもこれに対する解決策はありますか?