本文目錄一覽:
- 1、Java中怎麼讀取縮略圖?像Window中一樣,要高效,防止內存溢出! 如何分頁顯示!
- 2、有人知道java生成縮略圖的方法嗎 thumbnailator有問題
- 3、java根據url獲取網頁縮略圖
- 4、java JFileChooser選擇圖片時,如何可以看到圖片縮略圖,從而進行選擇?
Java中怎麼讀取縮略圖?像Window中一樣,要高效,防止內存溢出! 如何分頁顯示!
java內存溢出
核心提示:原因有很多種,比如: 1.數據量過於龐大;死循環 ;靜態變數和靜態方法過多;遞歸;無法確定是否被引用的對象; 2.虛擬機不回收內存(內存泄漏); 說白了就是程序運行要用到的內存大於虛擬機能提供的最大內存就發生內存溢出了。 內存溢出的問題要看業務和系
原因有很多種,比如:
1.數據量過於龐大;死循環 ;靜態變數和靜態方法過多;遞歸;無法確定是否被引用的對象;
2.虛擬機不回收內存(內存泄漏);
說白了就是程序運行要用到的內存大於虛擬機能提供的最大內存就發生內存溢出了。 內存溢出的問題要看業務和系統大小而定,對於某些系統可能內存溢出不常見,但某些系統還是很常見的解決的方法,
一個是優化程序代碼,如果業務龐大,邏輯複雜,盡量減少全局變數的引用,讓程序使用完變數的時候釋放該引用能夠讓垃圾回收器回收,釋放資源。
二就是物理解決,增大物理內存,然後通過:-Xms256m -Xmx256m -XX:MaxNewSize=256m -XX:MaxPermSize=256m的修改
有人知道java生成縮略圖的方法嗎 thumbnailator有問題
自己寫了一個,看看能不能有
/**
* 本類實現一個對JPG/JPEG圖像文件進行縮放處理的方法:即給定一個JPG文件,可以生成一個該JPG文件的縮影圖像文件(JPG格式)br/
* 提供三種生成縮影圖像的方法:br/
* 1.設置縮影文件的寬度, 根據設置的寬度和源圖像文件的大小來確定新縮影文件的長度來生成縮影圖像br/
* 2.設置縮影文件的長度, 根據設置的長度和源圖像文件的大小來確定新縮影文件的寬度來生成縮影圖像br/
* 3.設置縮影文件相對於源圖像文件的比例大小, 根據源圖像文件的大小及設置的比例來確定新縮影文件的大小來生成縮影圖像br/
* 新生成的縮影圖像可以比原圖像大, 這時即是放大源圖像br/
*
* @author 不落的太陽(Sean Yang)
* @version 1.0
* @since JDK 1.8
*
*/
public class ImageScalingTool {
// 對象是否己經初始化
private boolean isInitFlag = false;
// 定義生目標圖片的寬度和高度, 給其一個就可以了
private int targetPicWidth = 0;
private int targetPicHeight = 0;
// 定義目標圖片的相比原圖片的比例
private double picScale = 0;
/**
* 構造函數
*/
public ImageScalingTool() {
isInitFlag = false;
}
/**
* 重置JPG圖片縮放器
*/
public void resetImageScaling() {
picScale = 0;
targetPicWidth = 0;
targetPicHeight = 0;
isInitFlag = false;
}
/**
* 設置目標圖片相對於源圖片的縮放比例
*
* @param scale
* @throws JPEGException
*/
public void setPicScale(double scale) throws Exception {
if (scale = 0) {
throw new RuntimeException(” 縮放比例不能為0和負數! “);
}
resetImageScaling();
picScale = scale;
isInitFlag = true;
}
/**
* 設置目標圖片的寬度
*
* @param width
* @throws JPEGException
*/
public void setSmallWidth(int width) throws Exception {
if (width = 0) {
throw new RuntimeException(” 縮影圖片的寬度不能為 0 和負數! “);
}
resetImageScaling();
targetPicWidth = width;
isInitFlag = true;
}
/**
* 設置目標圖片的高度
*
* @param height
* @throws JPEGException
*/
public void setSmallHeight(int height) throws Exception {
if (height = 0) {
throw new RuntimeException(” 縮影圖片的高度不能為 0 和負數! “);
}
resetImageScaling();
targetPicHeight = height;
isInitFlag = true;
}
/**
* 開始縮放圖片
*
* @param srcPicFileName
* 源圖片的文件名
* @param targetPicFileName
* 生成目標圖片的文件名
* @throws JPEGException
*/
public void transform(String srcPicFileName, String targetPicFileName) throws Exception {
if (!isInitFlag) {
throw new RuntimeException(” 對象參數沒有初始化! “);
}
if (srcPicFileName == null || targetPicFileName == null) {
throw new RuntimeException(” 包含文件名的路徑為空! “);
}
if ((!srcPicFileName.toLowerCase().endsWith(“jpg”)) (!srcPicFileName.toLowerCase().endsWith(“jpeg”))) {
throw new RuntimeException(” 只能處理 JPG/JPEG 文件! “);
}
if ((!targetPicFileName.toLowerCase().endsWith(“jpg”)) !targetPicFileName.toLowerCase().endsWith(“jpeg”)) {
throw new RuntimeException(” 只能處理 JPG/JPEG 文件! “);
}
// 新建源圖片和生成圖片的文件對象
File fin = new File(srcPicFileName);
File fout = new File(targetPicFileName);
// 通過緩衝讀入源圖片文件
BufferedImage sourceImage = null;
try {
// 讀取文件生成BufferedImage
sourceImage = ImageIO.read(fin);
} catch (IOException ex) {
throw new RuntimeException(” 讀取源圖像文件出錯! “);
}
// 源圖片的寬度和高度
int sourceWidth = sourceImage.getWidth();
int sourceHeight = sourceImage.getHeight();
// 設置目標圖片的實際寬度和高度
int targetWidth = 0;
int targetHeight = 0;
if (targetPicWidth != 0) {
// 根據設定的寬度求出長度
targetWidth = targetPicWidth;
targetHeight = (targetWidth * sourceHeight) / sourceWidth;
} else if (targetPicHeight != 0) {
// 根據設定的長度求出寬度
targetHeight = targetPicHeight;
targetWidth = (targetHeight * sourceWidth) / sourceHeight;
} else if (picScale != 0) {
// 根據設置的縮放比例設置圖像的長和寬
targetWidth = (int) (sourceWidth * picScale);
targetHeight = (int) (sourceHeight * picScale);
} else {
throw new RuntimeException(” 對象參數初始化不正確! “);
}
System.out.println(” 源圖片的解析度: ” + sourceWidth + “×” + sourceHeight);
System.out.println(” 目標圖片的解析度: ” + targetWidth + “×” + targetHeight);
// 目標圖像的緩衝對象
BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_3BYTE_BGR);
// 求得目標圖片與源圖片寬度, 高度的比例.
double scaleWidth = (double) targetWidth / sourceWidth;
double scaleHeight = (double) targetHeight / sourceHeight;
// 構造圖像變換對象
AffineTransform transform = new AffineTransform();
// 設置圖像轉換的比例
transform.setToScale(scaleWidth, scaleHeight);
// 構造圖像轉換操作對象
AffineTransformOp ato = new AffineTransformOp(transform, null);
// 實現轉換, 將bSrc轉換成bTarget
ato.filter(sourceImage, targetImage);
// 輸出目標圖片
try {
// 將目標圖片的BufferedImage寫到文件中去, jpeg為圖片的格式
ImageIO.write(targetImage, “jpeg”, fout);
} catch (IOException ex1) {
throw new Exception(” 寫入縮略圖像文件出錯! “);
}
}
}
java根據url獲取網頁縮略圖
代碼如下:
public static Bitmap
loadImageFromUrl(String url, int sc) {
URL m;
InputStream
i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream out = null;
byte isBuffer[] = new
byte[1024];
if (url == null)
return null;
try {
m = new URL(url);
i = (InputStream)
m.getContent();
bis = new BufferedInputStream(i, 1024 * 4);
out =
new ByteArrayOutputStream();
int len = 0;
while
((len = bis.read(isBuffer)) != -1) {
out.write(isBuffer, 0,
len);
}
out.close();
bis.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
return null;
} catch
(IOException e) {
e.printStackTrace();
}
if
(out == null)
return null;
byte[] data =
out.toByteArray();
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inJustDecodeBounds =
true;
BitmapFactory.decodeByteArray(data, 0, data.length,
options);
options.inJustDecodeBounds = false;
int be =
(int) (options.outHeight / (float) sc);
if (be = 0)
{
be = 1;
} else if (be 3) {
be =
3;
}
options.inSampleSize = be;
Bitmap bmp =
null;
try {
bmp = BitmapFactory.decodeByteArray(data,
0, data.length, options); // 返回縮略圖
} catch (OutOfMemoryError e)
{
// TODO: handle exception
System.gc();
bmp = null;
}
return
bmp;
}
java JFileChooser選擇圖片時,如何可以看到圖片縮略圖,從而進行選擇?
菜單欄(後退那一排)最後面有個查看,那裡面點擊縮略圖,即可看到從而選擇幻燈片,縮略圖等
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/181463.html