导航:首页 > 编程语言 > java视频缩略图生成

java视频缩略图生成

发布时间:2022-10-24 09:40:34

① 如何用java为文件生成缩略图

public static boolean scale(String imagepath,String newpath){
// 返回一个 BufferedImage,作为使用从当前已注册 ImageReader 中自动选择的 ImageReader 解码所提供 File 的结果

BufferedImage image=null;
try {
image = ImageIO.read(new File(imagepath));
} catch (IOException e) {
System.out.println("读取图片文件出错!"+e.getMessage());
return false;
}

// Image Itemp = image.getScaledInstance(300, 300, image.SCALE_SMOOTH);
double Ratio = 0.0;

if ((image.getHeight() > 300) ||(image.getWidth() > 300)) {
if (image.getHeight() > image.getWidth())
//图片要缩放的比例
Ratio = 300.0 / image.getHeight();
else
Ratio = 300.0 / image.getWidth();
}
// 根据仿射转换和插值类型构造一个 AffineTransformOp。
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
// 转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
image = op.filter(image,null);
//image.getScaledInstance(300,300,image.SCALE_SMOOTH);

FileOutputStream out=null;
try {
out = new FileOutputStream(newpath);
ImageIO.write((BufferedImage)image,"bmp",out);
out.close();
} catch (Exception e) {
System.out.println("写图片文件出错!!"+e.getMessage());
return false;
}
return true;
}

② java上传图片 生成缩略图,如果上传的图片尺寸比较小就压缩处理

//将图按比例缩小。
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
//这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
//则将下面的if else语句注释即可
if(sx>sy)
{
sx = sy;
targetW = (int)(sx * source.getWidth());
}else{
sy = sx;
targetH = (int)(sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}

public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
throws Exception {
BufferedImage srcImage;
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
String imgType = "JPEG";
if (fromFileStr.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
// System.out.println(ex);
File saveFile=new File(saveToFileStr);
File fromFile=new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if(width > 0 || hight > 0)
{
srcImage = resize(srcImage, width, hight);
}
ImageIO.write(srcImage, imgType, saveFile);

}

public static void main (String argv[]) {
try{
//参数1(from),参数2(to),参数3(宽),参数4(高)
saveImageAsJpg("C:\\Documents and Settings\\xugang\\桌面\\tmr-06.jpg",
"C:\\Documents and Settings\\xugang\\桌面\\2.jpg",
120,120);
} catch(Exception e){
e.printStackTrace();
}

}

③ java如何截取视频的帧,生成一个图片

就是可以使用ffmpeg,下载ffmpeg相关组件到电脑中,然后用java调用命令的方式(RunTime.exec相关方法),使用ffmpeg的功能。

④ Java编程:怎么获得一个视频的缩略图呢

如果本地视频的话,可以通过Runtime类的exec方法调用ffmpeg来实现
ffmpeg是视频转码,截图的程序,我这里有

⑤ 视频缩略图是根据什么原理生成,获取的

本文实例讲述了C#获取视频某一帧的缩略图的方法。分享给大家供大家参考。具体实现方法如下:
读取方式:使用ffmpeg读取,所以需要先下载ffmpeg。网上资源有很多。
原理是通过ffmpeg执行一条命令获取视频某一帧的缩略图。

⑥ 你好 分享视频 如何获得缩略图有没有 java 的方法 谢谢

就缩小就行了

⑦ 有什么工具或播放器可以生成视频缩略图

很多PT站九宫格预览截图上常年显示的是射手影音的Logo..

不过好像新版本样式有很大的变化,设计上做了精简,截了几张喜欢的影片可以看一下效果。

还挺好用的,截图很清晰,该有的视频信息也有,目前有九宫格和十六宫格两种导出,能满足常规需求了。

射手影音 SPlayer


⑧ Java编写获得指定视频的缩略图

下载个ffmpeg.exe 用java 调用ffmpeg进程 ffmpeg是可以命令行操作的 可以去查下ffmpeg的命令选项

⑨ 怎样为多媒体文件生成缩略图

多媒体文件分为视频、音频、图片,下面分类介绍多媒体文件生成缩略图的方法:
1、Video
对于视频,取第一帧作为缩略图,也就是怎样从filePath得到一个Bitmap对象。
private Bitmap createVideoThumbnail(String filePath) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
retriever.setDataSource(filePath);
bitmap = retriever.captureFrame();
} catch(IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
return bitmap;
}
Android提供了MediaMetadataRetriever,由JNI(media_jni)实现。
看得出MediaMetadataRetriever主要有两个功能:MODE_GET_METADATA_ONLY和MODE_CAPTURE_FRAME_ONLY
这里设mode为MODE_CAPTURE_FRAME_ONLY,调用captureFrame取得一帧。
另外还有两个方法可以用:
extractMetadata 提取文件信息,ARTIST、DATE、YEAR、DURATION、RATING、FRAME_RATE、VIDEO_FORMAT
和extractAlbumArt 提取专辑信息,这个下面的音乐文件可以用到。

2、Music
对于音乐,取得AlbumImage作为缩略图,还是用MediaMetadataRetriever
private Bitmap createAlbumThumbnail(String filePath) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
retriever.setDataSource(filePath);
byte[] art = retriever.extractAlbumArt();
bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
} catch(IllegalArgumentException ex) {
} catch (RuntimeException ex) {
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
return bitmap;
}
retriever.extractAlbumArt()得到的是byte数组,还需要一步用BitmapFactory编码得到Bitmap对象。

3、Image
图片就很简单了
Bitmap bm = null;
Options op = new Options();
op.inSampleSize = inSampleSize;
op.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(mFile.getPath(), op);
能直接得到Bitmap对象,把图片缩小到合适大小就OK。
同样上面的Video和Music,retrive到Bitmap后也需要缩小处理。

Powered by ScribeFire.

⑩ javaWeb怎么实现根据内容生成缩略图

packagecom.hoo.util;

importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.MalformedURLException;
importjava.net.URL;
importjavax.imageio.ImageIO;
importcom.sun.image.codec.jpeg.ImageFormatException;
importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGEncodeParam;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;

/**
*<b>function:</b>缩放图片工具类,创建缩略图、伸缩图片比例
*@authorhoojo
*@createDate2012-2-3上午10:08:47
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@version1.0
*/
{

_SCALE_QUALITY=1f;
_IMAGE_FORMAT=".jpg";//图像文件的格式
_FILE_PATH="C:/temp-";

/**
*<b>function:</b>设置图片压缩质量枚举类;
*Someguidelines:0.75highquality、0.5mediumquality、0.25lowquality
*@authorhoojo
*@createDate2012-2-7上午11:31:45
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@projectJQueryMobile
*@version1.0
*/
publicenumImageQuality{
max(1.0f),high(0.75f),medium(0.5f),low(0.25f);

privateFloatquality;
publicFloatgetQuality(){
returnthis.quality;
}
ImageQuality(Floatquality){
this.quality=quality;
}
}

privatestaticImageimage;

/**
*<b>function:</b>通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
*@authorhoojo
*@createDate2012-2-6下午04:41:48
*@paramtargetWidth目标的宽度
*@paramtargetHeight目标的高度
*@paramstandardWidth标准(指定)宽度
*@paramstandardHeight标准(指定)高度
*@return最小的合适比例
*/
publicstaticdoublegetScaling(doubletargetWidth,doubletargetHeight,doublestandardWidth,doublestandardHeight){
doublewidthScaling=0d;
doubleheightScaling=0d;
if(targetWidth>standardWidth){
widthScaling=standardWidth/(targetWidth*1.00d);
}else{
widthScaling=1d;
}
if(targetHeight>standardHeight){
heightScaling=standardHeight/(targetHeight*1.00d);
}else{
heightScaling=1d;
}
returnMath.min(widthScaling,heightScaling);
}

阅读全文

与java视频缩略图生成相关的资料

热点内容
原神安卓区服什么意思 浏览:34
贝壳app怎么线上发布 浏览:157
如何挑选安卓系统机顶盒 浏览:53
安卓快充使用有什么注意事项 浏览:909
黑马程序员的云计算网课 浏览:946
endnotestyle文件夹怎么导入 浏览:460
讲解少儿编程演讲会开头 浏览:424
思科交换机基础命令 浏览:497
便签可以设置加密吗 浏览:339
免费漫画app怎么看书 浏览:27
华为笔记本电脑怎么安装抖音app 浏览:412
阿里云国际版试用的服务器怎么搞 浏览:895
java正则表达式工具 浏览:160
oa服务器怎么设置ftp 浏览:10
安卓如何安装obb 浏览:442
QQ聊天记录journal文件夹 浏览:118
苹果公司云服务器地址 浏览:85
加密记事本手机 浏览:437
汽车压缩机变频阀 浏览:95
域外服务器是什么意思 浏览:639