如何在不重新运行方法的情况下更新方法中的字节数组?

时间:2016-02-18 14:21:44

标签: java android methods ffmpeg byte

我有一个类(AsyncTask)进行图像处理并连续生成yuv字节,间隔约为200毫秒。

现在我将这些yuv字节发送到另一个使用FFmpeg帧记录器记录它们的方法:

public void recordYuvData() {

        byte[] yuv = getNV21();
        System.out.println(yuv.length + "  returned yuv bytes  ");
        if (audioRecord == null || audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
            startTime = System.currentTimeMillis();
            return;
        }
        if (RECORD_LENGTH > 0) {
            int i = imagesIndex++ % images.length;
            yuvimage = images[i];
            timestamps[i] = 1000 * (System.currentTimeMillis() - startTime);
        }
        /* get video data */
        if (yuvimage != null && recording) {
            ((ByteBuffer) yuvimage.image[0].position(0)).put(yuv);

            if (RECORD_LENGTH <= 0) {
                try {
                    long t = 1000 * (System.currentTimeMillis() - startTime);
                    if (t > recorder.getTimestamp()) {
                        recorder.setTimestamp(t);
                    }
                    recorder.record(yuvimage);
                } catch (FFmpegFrameRecorder.Exception e) {

                    e.printStackTrace();
                }
            }
        }
    }

这种方法;单击按钮时会启动recordYuvData()。

  1. 如果我只启动一次,那么只记录初始图像,其余部分则不记录。

  2. 如果我每次在图像处理结束后启动它,它会记录但会导致“奇怪”。 fps计算视频;最后这会导致应用程序崩溃。

    对于上面我的感觉,在图像处理结束时,创建了一个新的recordYuvData()实例而不结束前一个实例,累积了许多recordYuvData()实例。 [如果我错了,请纠正我]

  3. 那么,我如何更新&#39;仅限&#39;方法中的yuv字节没有再次运行它?

    ...谢谢!

    修改

    点击:

        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recordYuvdata();
                startRecording();
    

    getNV21()

    byte[] getNV21(Bitmap bitmap) {
    
        int inputWidth = 1024;
        int inputHeight = 640;
        int[] argb = new int[inputWidth * inputHeight];
    
        bitmap.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
        System.out.println(argb.length + "@getpixels ");
    
    
        byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
        encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
    
        return yuv;
    
    }
    
    void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
        final int frameSize = width * height;
    
        int yIndex = 0;
        int uvIndex = frameSize;
        System.out.println(yuv420sp.length + " @encoding " + frameSize);
    
        int a, R, G, B, Y, U, V;
        int index = 0;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {
    
                a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
                R = (argb[index] & 0xff0000) >> 16;
                G = (argb[index] & 0xff00) >> 8;
                B = (argb[index] & 0xff) >> 0;
    
                // well known RGB to YUV algorithm
    
                Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
                U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
                V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
    
                // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
                //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
                //    pixel AND every other scanline.
                yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
                if (j % 2 == 0 && index % 2 == 0) {
                    yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
                    yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
                }
    
                index++;
            }
        }
    }
    

0 个答案:

没有答案