導航:首頁 > 編程語言 > java輸出圖片流

java輸出圖片流

發布時間:2022-06-23 07:29:28

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圖片輸出

等著拿分.......
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
* @author alanwei
*
*/
public class Test {

public static BufferedImage createImage(int width, int height, String s) {
Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);

FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;

g2.drawString(s, (int)x, (int)baseY);

return bi;
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedImage image = createImage(100, 20, "123456789");

File file = new File("image.jpg");
if (!file.exists()) {
file.createNewFile();
}

if (image != null) {
ImageIO.write(image, "jpg", file);
}
}
}

③ JAVA如何直接在console控制台上用IO流輸出圖片急求!

解決方法:int len = fis.read(); read 方法加入參數bys,這樣才能把fis的內容注入bys裡面。

順便說下,FileInputStream不能正確輸出中文,因為這個是按位元組輸出的,每個中文站2個位元組,會出現亂碼。

④ 怎樣用JAVA編寫一個小應用程序,輸出一張圖片或自己的照片

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class text
{
JFrame jf;
JLabel jl;
JPanel jp1;
public static void main(String[] args)
{
text t=new text();
t.go();
}
void go()
{
final ImageIcon image1 = new ImageIcon("1.gif");
jf=new JFrame();
jl = new JLabel(image1);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 300);
jf.setLocation(400, 300);
jp1 = new JPanel();
jp1.add(jl);
jf.getContentPane().add(jp1);
jf.setVisible(true);
}
}
把你的找一張gif 格式的照片 .名字改成1.gif 放在你的項目根目錄下

⑤ 請問 用java語句輸出如圖片圖案應該怎麼做

1、代碼如下:

public class Main

{

public static void main(String[] args) {

System.out.println("Hello World!");

//主循環

for(int i =10;i>0;i--){

//輸出空格

for(int k=i;k>0;k--){System.out.print(" ");}

//輸出數字

for(int j=i;j<=10;j++){

System.out.print(j+" ");

}System.out.println(" ");

}

}

}

2、效果如圖

⑥ JAVA 輸出圖片代碼

假如只在當前頁面顯示的話直接用 img src
在HTML(或者jsp)的body 里添加
<img src="test/1.jpg" />,
以上述例子說明,你現在所編輯的頁面是index.jsp,那麼test(文件夾)是跟此頁面在同一目錄中的,1.jpg是需要顯示的圖片,位置在test文件夾下。
總的意思是:文件夾與頁面並列,圖片在文件夾下。

⑦ java怎麼在控制台輸出一張jpg的圖片

輸出圖片的base64編碼

//imgFile是圖片的路徑
publicstaticvoidgetImageStr(StringimgFile){
InputStreaminputStream=null;
byte[]data=null;
try{
inputStream=newFileInputStream(imgFile);
data=newbyte[inputStream.available()];
inputStream.read(data);
inputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}//加密
BASE64Encoderencoder=newBASE64Encoder();
System.out.println(encoder.encode(data));
}

⑧ java把圖片轉換成二進制流

public static void main(String[] args) throws Exception {

File file = new File("d:\L.jpg");//圖片

FileInputStream fis = new FileInputStream(file);//把圖片變成流

FileOutputStream fos = new FileOutputStream(new File("E:\L.jpg")); //把圖片流寫入E盤

byte[] read = new byte[1024]; //每次讀取的位元組 可以自己定義 256 512 1024 2048 等。。。

int len = 0;

while((len = fis.read(read))!= -1){ //讀取變成流的圖片

fos.write(read,0,len);//寫入圖片

}

fis.close();//關閉輸入流

fos.close();//關閉輸出流

}

⑨ java中輸出圖片的代碼

final ImageView iv=(ImageView)findViewById(R.id.iv);
Button bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View p1)
{
// TODO: Implement this method
if(iv.getDrawable()!=null)
iv.setImageResource(R.id.photo);
else iv.setImageResource(0);
}
});

⑩ Java編寫時,怎麼輸出一張圖片呀

(1)首先用ImageIO類讀取這張圖片
(2)如果要對這張圖片修改,通過圖片獲取Graphics對象,再調用Graphics的方法來繪制、修改。

(3)再調用ImageIO的方法將圖片輸出到特定IO流即可。

具體代碼實例可參考李剛的瘋狂Java講義的11.8節。

閱讀全文

與java輸出圖片流相關的資料

熱點內容
python列表求交集 瀏覽:872
解壓包如何轉音頻 瀏覽:447
機明自動編程軟體源碼 瀏覽:325
php埠號設置 瀏覽:541
phperegreplace 瀏覽:320
androidgridview翻頁 瀏覽:537
ssh協議編程 瀏覽:634
如何開我的世界電腦伺服器地址 瀏覽:861
玄關pdf 瀏覽:609
程序員學習論壇 瀏覽:940
程序員的毒雞湯怎麼做 瀏覽:548
安卓怎麼降級軟體到手機 瀏覽:281
雲與伺服器入門書籍推薦產品 瀏覽:636
delphi編程助手 瀏覽:762
電腦遇到伺服器問題怎麼辦 瀏覽:515
加工中心編程結束方法 瀏覽:296
了解什麼是web伺服器 瀏覽:140
面向對象的編程的基本特徵 瀏覽:718
php定時執行任務linux 瀏覽:787
php數組中刪除元素 瀏覽:725