【懒人精灵】将bitmap图片压缩和转换为H.264帧


import('android.media.MediaCodec')
import('android.media.MediaFormat')
import('android.media.MediaCodecInfo')
import('java.nio.ByteBuffer')
import('java.lang.reflect.Array')
import('android.graphics.Bitmap')

function createEncoder(width, height, bitRate, frameRate)
    local format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, width, height)
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface)
    format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate)
    format.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate)
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1)

    local encoder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC)
    encoder.configure(format, nil, nil, MediaCodec.CONFIGURE_FLAG_ENCODE)
    return encoder
end

function bitmapToH264Frame(bitmap, encoder)
    -- 获取输入缓冲区
    local inputBufferIndex = encoder.dequeueInputBuffer(-1)
    if inputBufferIndex >= 0 then
        local inputBuffer = encoder.getInputBuffer(inputBufferIndex)
        inputBuffer.clear()

        -- 获取bitmap的尺寸
        local width = bitmap.getWidth()
        local height = bitmap.getHeight()

        -- 创建像素数组
        local pixels = {}
        for y = 0, height - 1 do
            for x = 0, width - 1 do
                local color = bitmap.getPixel(x, y)
                table.insert(pixels, color)
            end
        end

        -- 将像素数据转换为字节数组
        local bytes = {}
        for i = 1, #pixels do
            local color = pixels[i]
            table.insert(bytes, (color >> 16) & 0xFF) -- R
            table.insert(bytes, (color >> 8) & 0xFF)  -- G
            table.insert(bytes, color & 0xFF)         -- B
            table.insert(bytes, (color >> 24) & 0xFF) -- A
        end

        -- 将字节数组写入输入缓冲区
        for i = 1, #bytes do
            inputBuffer.put(bytes[i])
        end

        -- 提交编码请求
        encoder.queueInputBuffer(inputBufferIndex, 0, #bytes,
            System.nanoTime() / 1000, 0)
    end

    -- 获取编码后的数据
    local outputBufferIndex = encoder.dequeueOutputBuffer(nil, 0)
    if outputBufferIndex >= 0 then
        local outputBuffer = encoder.getOutputBuffer(outputBufferIndex)
        local encodedData = {}
        for i = 0, outputBuffer.remaining() - 1 do
            table.insert(encodedData, outputBuffer.get(i))
        end
        encoder.releaseOutputBuffer(outputBufferIndex, false)
        return encodedData
    end
    return nil
end

-- 使用示例
local width = 720
local height = 1280
local frameRate = 30
local bitRate = 4000000

local encoder = createEncoder(width, height, bitRate, frameRate)
encoder.start()

local bitmap = LuaEngine.snapShot(0, 0, 0, 0)
-- 第一个参数:原始位图对象
-- 第二个参数:缩放后的宽度
-- 第三个参数:缩放后的高度
-- 第四个参数:是否进行滤波处理(true表示进行滤波,可以提高缩放质量)
local scaleBitmap = Bitmap.createScaledBitmap(bitmap, width,height, true);
local h264Frame = bitmapToH264Frame(scaleBitmap, encoder)
print(h264Frame)
encoder.stop()
encoder.release()
releaseBmp(bitmap)
1. 官方交流QQ群,添加多个不批。建议使用安卓手机或电脑申请。
飞云脚本圈: 586333520飞云脚本圈
Auto.js学习交流③群:286635606
Auto.js学习交流②群:712194666(满员)
IOS免越狱自动化测试群:691997586
2. 盗版,破解有损他人权益和违法作为,请各位会员支持正版。
3. 本站部分资源来源于用户上传和网络搜集,如有侵权请提供版权证明并联系站长删除。
4.如未特别申明,本站的技术性文章均为原创,未经授权,禁止转载/搬运等侵权行为。
5.全站所有付费服务均为虚拟商品,购买后自动发货。售出后概不接受任何理由的退、换。注册即为接受此条款。
6.如果站内内容侵犯了您的权益,请联系站长删除。
飞云脚本 » 【懒人精灵】将bitmap图片压缩和转换为H.264帧

企业级大数据智能营销管理系统

源码转让