『壹』 java GUI 中如何將一張圖片讀到一個byte數組或其他數組中,有如何將該數組再轉換成圖片
/**
* 轉換Image數據為byte數組
* @param image
* Image對象
* @param format
* image格式字元串.如"gif","png"
* @return byte數組
*/
public static byte[] imageToBytes(Image image, String format) {
BufferedImage bImage = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, format, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/**
* 轉換byte數組為Image
* @param bytes
* @return Image
*/
public static Image bytesToImage(byte[] bytes) {
Image image = Toolkit.getDefaultToolkit().createImage(bytes);
try {
MediaTracker mt = new MediaTracker(new Label());
mt.addImage(image, 0);
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
return image;
}
『貳』 有關java byte類型的二維數組問題
首先:無
其次:
guave
Files.toByteArray(File)
ByteStreams.toByteArray(InputStream)
apache commons
IOUtils.toByteArray(InputStream input)
『叄』 java怎樣把一個byte數組保存成圖片到硬碟上
轉成的圖片 要麼是個文件File 要麼是個文件流. 那都只需要通過輸入輸出流往磁碟上寫就行了
『肆』 java中byte[]是怎麼意思
byte即位元組的意思,是java中的基本類型,用心申明位元組型的變數。
通常在讀取非文本文件時(如圖片,聲音,可執行文件)需要用位元組數組來保存文件的內容。
在下載文件時,也是用byte數組作臨時的緩沖器接收文件內容。所以說byte在文件操作時是必不可少的。不管是對文件寫入還是讀取都要用到。
(4)java圖片byte數組擴展閱讀:
Java具有簡單性、面向對象、分布式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
Java語言是一門隨時代快速發展的計算機語言程序,其深刻展示了程序編寫的精髓,加上其簡明嚴謹的結構及簡潔的語法編寫為其將來的發展及維護提供了保障。
由於提供了網路應用的支持和多媒體的存取,會推動Internet和企業網路的Web的應用。
『伍』 為什麼java中我把一張圖片讀到一個byte數組中,然後把該數組個元素加一,再把數組還原成圖片時會不行
應該是類型轉換問題,也就是說從一開始你要是使用byte,那麼第一種方法是可行的,可能你是從中途把別的類型的要轉換成byte,當然不行了,因為比如說,老虎一定是動物,但是動物不一定是老虎,所以你先看看你最前面的是不是定義的比byte大的類型了
『陸』 Java中如何把圖片轉換成二進制流
Java中將圖片轉為二進制流只需要使用FileImageInputStream取得圖片文件,然後使用ByteArrayOutputStream 寫入到二進制流中即可,下面是詳細代碼:
//圖片到byte數組
publicbyte[]image2byte(Stringpath){
byte[]data=null;
FileImageInputStreaminput=null;
try{
input=newFileImageInputStream(newFile(path));
ByteArrayOutputStreamoutput=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intnumBytesRead=0;
while((numBytesRead=input.read(buf))!=-1){
output.write(buf,0,numBytesRead);
}
data=output.toByteArray();
output.close();
input.close();
}
catch(FileNotFoundExceptionex1){
ex1.printStackTrace();
}
catch(IOExceptionex1){
ex1.printStackTrace();
}
returndata;
}
另外,如果需要將byte[]存回圖片或轉為String,則:
//byte數組到圖片
publicvoidbyte2image(byte[]data,Stringpath){
if(data.length<3||path.equals(""))return;
try{
=newFileImageOutputStream(newFile(path));
imageOutput.write(data,0,data.length);
imageOutput.close();
System.out.println("MakePicturesuccess,Pleasefindimagein"+path);
}catch(Exceptionex){
System.out.println("Exception:"+ex);
ex.printStackTrace();
}
}
//byte數組到16進制字元串
publicStringbyte2string(byte[]data){
if(data==null||data.length<=1)return"0x";
if(data.length>200000)return"0x";
StringBuffersb=newStringBuffer();
intbuf[]=newint[data.length];
//byte數組轉化成十進制
for(intk=0;k<data.length;k++){
buf[k]=data[k]<0?(data[k]+256):(data[k]);
}
//十進制轉化成十六進制
for(intk=0;k<buf.length;k++){
if(buf[k]<16)sb.append("0"+Integer.toHexString(buf[k]));
elsesb.append(Integer.toHexString(buf[k]));
}
return"0x"+sb.toString().toUpperCase();
}
『柒』 java byte類型有什麼用
存儲位元組用,後面還會用到位元組數組,再位元組流的時候
『捌』 用java寫個程序 將 圖像文件(png,jpg,等等) 轉換成byte數組的 程序 謝謝 了
/* 讀寫圖像文件 */
import javax.swing.JOptionPane;
import java.io.*;
class FileRW
{
int bytes;
byte buffer[ ] = new byte[65560];
FileInputStream fileInput;
FileOutputStream fileOutput;
FileRW()
{
takeimg();
loadimg();
JOptionPane.showMessageDialog(null,"文件復制並更名成功!\n文件大小為:"+bytes);
System.exit(0); //退出程序
}
//讀取圖像文件a.jpg
public void takeimg()
{
try {
File file=new File("a.jpg");
fileInput = new FileInputStream(file);
bytes = fileInput.read(buffer,0,65560);
} catch(IOException ei) { System.out.println(ei); }
}
//寫入到b.jpg
public void loadimg()
{
try {
fileOutput = new FileOutputStream("b.jpg");
fileOutput.write(buffer, 0, bytes) ;
} catch(IOException eo) { System.out.println(eo); }
}
}
public class 讀寫圖像文件
{
public static void main(String[] args)
{ new FileRW(); }
}
『玖』 JAVA 如何讀取接受到的byte[]圖片
同上