使用aforge的AVIWriter打开或创建文件异常Failed creating compressed stream的解决办法

本文介绍如何使用AForge.NET框架中的AVIReader和AVIWriter类进行AVI视频文件的读取与写入操作。通过示例代码展示了打开、读取及关闭AVI文件的过程,并提供了创建AVI文件并添加帧的具体步骤。

参考http://www.aforgenet.com/framework/features/avi_files.html

Reading/writing AVI files

AForge.NET framework provides simple API to read and write AVI video files throughVideo for Windows interface. Although the interface is quite old and marked as obsolete by Microsoft, it is still supported and gives fairly simple API for accessing AVI video files.

AVIReader class
The class provides access to AVI video files and allows getting each video frame individually, as well as navigate through a video file specifying index of the next frame to receive:

// instantiate AVI reader
AVIReader reader = new AVIReader( );
// open video file
reader.Open( "test.avi" );
// read the video file
while ( reader.Position - reader.Start < reader.Length )
{
    // get next frame
    Bitmap image = reader.GetNextFrame( );
    // .. process the frame somehow or display it
}
reader.Close( );

AVIWriter class
The class provides simple API for writing AVI video files. All you need to do is to specify codec to use, video size and frame rate and then start adding frames: 

// instantiate AVI writer, use WMV3 codec
AVIWriter writer = new AVIWriter( "wmv3" );
// create new AVI file and open it
writer.Open( "test.avi", 320, 240 );
// create frame image
Bitmap image = new Bitmap( 320, 240 );

for ( int i = 0; i < 240; i++ )
{
    // update image
    image.SetPixel( i, i, Color.Red );
    // add the image as a new frame of video file
    writer.AddFrame( image );
}
writer.Close( );

使用writer.Open( "test.avi", 320, 240 );的时候,抛出异常Failed creating compressed stream

原因是:(1)wmv3编码不能在64bit下运行(2)windows7没有相应的codec

解决方法:(1)将所有的项目,改为x86 (2)安装wmv9VCMsetup (下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=6191) 本人由于是32位的win7系统,只安装了Winndows Media Video 9 VCM就解决了问题。


感谢 Aforge开发摄像头监控的一些情况说明 http://www.cnblogs.com/ddlzq/archive/2010/10/21/1857385.html




在 Android 开发中,`Bitmap` 是图像处理中最常用的数据结构,但也是内存消耗大户。为了优化内存使用和提升性能,**Bitmap 压缩策略**非常重要。下面是常见的 Bitmap 压缩策略及使用方法: --- ### 一、Bitmap 压缩策略分类 #### 1. **质量压缩(Quality Compression)** **原理**:通过降低图片的 JPEG 质量来减少文大小,不改变图片的尺寸。 **适用场景**:保存图片到本地、上传图片等。 ```java Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 100 表示不压缩,数值越小压缩率越高 bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos); byte[] compressedData = baos.toByteArray(); // 如果需要再转成 Bitmap Bitmap compressedBitmap = BitmapFactory.decodeByteArray(compressedData, 0, compressedData.length); ``` > ⚠️ 注意:JPEG 支持质量压缩,PNG 不支持(因为 PNG 是无损压缩)。 --- #### 2. **尺寸压缩(Scaling)** **原理**:按比例缩小图片的宽高,减少像素数量,从而降低内存占用。 **适用场景**:显示缩略图、头像等不需要高清图的场景。 ```java Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); float scale = 0.5f; // 缩小为原来的一半 Matrix matrix = new Matrix(); matrix.setScale(scale, scale); Bitmap scaledBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true); ``` --- #### 3. **采样率压缩(inSampleSize)** **原理**:加载图片时设置 `BitmapFactory.Options.inSampleSize`,按比例加载图片,减少内存占用。 **适用场景**:从资源文本地加载大图时。 ```java BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.drawable.large_image, options); // 设置 inSampleSize 为 2,表示宽高各缩小为原来的 1/2,像素数变为 1/4 options.inJustDecodeBounds = false; options.inSampleSize = calculateInSampleSize(options, 1024, 768); Bitmap sampledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_image, options); // 计算 inSampleSize 的方法 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int width = options.outWidth; final int height = options.outHeight; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfWidth = width / 2; final int halfHeight = height / 2; while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight) { inSampleSize *= 2; } } return inSampleSize; } ``` --- #### 4. **Bitmap 内存复用(BitmapPool)** **原理**:复用已有的 Bitmap 对象,避免频繁建和回收,减少内存抖动。 **适用场景**:频繁加载图片的场景(如 RecyclerView)。 ```java Bitmap reusableBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); BitmapFactory.Options options = new BitmapFactory.Options(); options.inBitmap = reusableBitmap; // 复用该 Bitmap options.inJustDecodeBounds = false; Bitmap decodedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options); ``` > ⚠️ 注意:`inBitmap` 要求复用的 Bitmap 必须是可变的(mutable),并且格式兼容。 --- ### 二、综合使用策略建议 | 场景 | 推荐策略 | |------|----------| | 显示缩略图 | 尺寸压缩 + inSampleSize | | 图片上传 | 质量压缩 + 尺寸压缩 | | 加载大图 | inSampleSize + BitmapFactory | | 频繁加载图片 | inBitmap + inSampleSize | --- ### ✅ 总结 | 压缩方式 | 是否改变尺寸 | 是否减少内存占用 | 是否影响画质 | 适用场景 | |----------|---------------|------------------|----------------|----------| | 质量压缩 | 否 | 否(内存)/是(文) | 是 | 图片保存、上传 | | 尺寸压缩 | 是 | 是 | 是 | 显示缩略图 | | inSampleSize | 是 | 是 | 是 | 加载大图 | | inBitmap | 否 | 是(减少频繁分配) | 否 | 高频图片加载 | ---
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值