當保存圖像時,它會變成黑色方塊而不是選擇的圖像,我希望能夠從我的計算機中保存圖像并將其保存在項目的文件夾中,以便在壓縮和發送時他們可以查看我上傳的圖像。我試過BufferedImage.TYPE_INT_ARGB了,但我不知道這是否是問題所在。private void imageToArray(){ int width = originalBI.getWidth(); int height = originalBI.getHeight(); newBI = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB); pixels = new int[width][height]; for(int i = 0; i<width;i++){ for(int j = 0;j<height;j++){ pixels[i][j]=originalBI.getRGB(i,j); } }}private void saveImage(){ int returnValue = saveFileChooser.showSaveDialog(this); if(returnValue == JFileChooser.APPROVE_OPTION) { try{ ImageIO.write(newBI, "png",saveFileChooser.getSelectedFile()); lblMessage.setText("Image File Succesfully saved"); }catch(IOException ex){ lblMessage.setText("Failed to save image file"); } } else{ lblMessage.setText("No file Choosen"); }}
1 回答

胡子哥哥
TA貢獻1825條經驗 獲得超6個贊
不需要逐個像素地工作,這會很慢。
private void imageToArray(){
int width = originalBI.getWidth();
int height = originalBI.getHeight();
newBI = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newBI.createGraphics();
g.drawImage(originalBI, 0, 0, width, height, null);
g.dispose();
}
可以創建一個圖形來繪制。
有多種方法,例如在圖像的一部分透明時使用背景顏色。
添加回答
舉報
0/150
提交
取消