Android Bitmap/View生成Mp4视频文件

bitmapsToMp4 with mediaCodec

Android Lib, bitmaps convert to mp4 with mediaCodec.

Bitmap或者View转Mp4视频
如果是View的话,提供了

YapVideoUtils.convertViewToBitmap(findViewById(R.id.img_test))

方法
可以方便的把View专成bitmap

github

代码我已经开源,喜欢的朋友可以直接去github看,直接引用就可以用了
想看效果的话就clone下来直接run即可
https://github.com/YaphetZhao/bitmapsToMp4

引用

How to
To get a Git project into your build:

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

   allprojects {
      repositories {
         ...
         maven { url 'https://jitpack.io' }
      }
   }

Step 2. Add the dependency

   dependencies {
           implementation 'com.github.YaphetZhao:bitmapsToMp4:1.0'
   }

使用方法

1. 集成方法

Activity继承:IYapVideoProvider
重写方法

    override fun size(): Int {
        return 视频总帧数
    }

    override fun next(): Bitmap {
        return 下一帧的bitmap
    }
    
    override fun progress(progress: Float) {
        runOnUiThread {
            if (progress >= 1f) {
                // 当 progress>=1f 视频保存完成
                val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                val uri: Uri = Uri.fromFile(File(latestResultPath))
                intent.data = uri
                sendBroadcast(intent)
            }
            val 进度 = """
                Save Progress: ${progress * 100}%
                """.trimIndent()
        }
    }

2. 开始保存视频

    // 获取手机相册目录
    private val galleryPath: String
        get() {
            val result = (Environment.getExternalStorageDirectory().toString()
                    + File.separator + Environment.DIRECTORY_DCIM
                    + File.separator + "Camera" + File.separator)
            val file = File(result)
            if (!file.exists()) {
                file.mkdir()
            }
            return result + File.separator
        }
    // 创建输出文件File文件名称,根据自己的需求改一下
    latestResultPath = "${galleryPath}YapBitmapToMp4_${System.currentTimeMillis()}.mp4"
    // 开始保存视频,可以在主线程调用,也可以在线程中调用,方法里面做了判断
    YapVideoEncoder(this, File(latestResultPath)).start()

其他

可以clone这个项目,运行app来查看demo效果

如果有问题可以联系我

微信:yaphetzhao92

邮箱:yaphetzhao@foxmail.com

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
你可以使用ZXing库来在Android生成二维码。首先,确保你在项目的`build.gradle`文件中添加了以下依赖项: ```groovy implementation 'com.google.zxing:core:3.4.0' implementation 'com.journeyapps:zxing-android-embedded:3.4.0' ``` 接下来,在你的Activity中,你可以使用以下代码生成二维码: ```java import android.graphics.Bitmap; import android.os.Bundle; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class MainActivity extends AppCompatActivity { private static final String CONTENT = "Hello, World!"; // 要生成二维码的内容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView qrCodeImageView = findViewById(R.id.qr_code_image_view); try { Bitmap bitmap = generateQrCode(CONTENT, 500, 500); // 生成二维码 qrCodeImageView.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } private Bitmap generateQrCode(String content, int width, int height) throws WriterException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } return bitmap; } } ``` 上述代码将在你的布局文件中显示一个带有生成的二维码的ImageView。你可以根据需要调整生成的二维码的大小和内容。记得在AndroidManifest.xml文件中添加相应的权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 这样你就可以在Android生成二维码了。希望对你有所帮助!
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值