7

以下是一些有关视频录制的链接:

如何在 Android 上捕获视频录制? https://github.com/churnlabs/android-ffmpeg-sample

还有很多关于视频录制的链接,但不知道如何使用远程 IP 摄像机录制视频。通过在stackoverflow上使用不同的样本,我可以拍照并保存在sdcard上,但无法录制视频。

如果有人有任何想法或代码以及所需的文件,我将不胜感激。

例如,我在 IP 摄像机可用的地方使用的 url 如下所示:

http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg/video.cgi?resolution=800x600&%3bdummy=1333689998337

这是布局代码:

<RelativeLayout
    android:id="@+id/LinearLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/logo"
    android:layout_alignParentTop="true"
    android:layout_weight="1" >

    <ImageButton
        android:id="@+id/btnCam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/camera_icon" >
    </ImageButton>

        <ImageButton
        android:id="@+id/btnVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/camera_icon" >
    </ImageButton>
</RelativeLayout>

<LinearLayout
    android:id="@+id/LinearLayout03"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/LinearLayout01"
    android:layout_below="@+id/LinearLayout02"
    android:layout_weight="1" >

    <RelativeLayout
        android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ffffff" >

        <view
            android:id="@+id/mv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            class="com.apps.GrahamConst.MjpegView" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"

    android:layout_weight="1"
    android:background="@drawable/navbar" >

    <ImageButton
        android:id="@+id/btnPrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="1"
        android:background="@null"
        android:src="@drawable/previous" >
    </ImageButton>

    <ImageButton
        android:id="@+id/btnMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="1"
        android:background="@null"
        android:src="@drawable/main" >
    </ImageButton>

    <ImageButton
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="1"
        android:background="@null"
        android:src="@drawable/next" >
    </ImageButton>
  </LinearLayout>

 </RelativeLayout>

这是我的活动

    public class CameraDetails2 extends Activity implements OnClickListener {

private MediaScannerConnection m_pScanner;
String drawable = null;
private MjpegView mv;
private ProgressDialog dialog;
private HashMap<String, String> item;
private int id = -1;
private WindowManager winMan;
boolean recording = false;
MediaRecorder recorder;

int bytearraysize = 0;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    winMan = (WindowManager) getApplicationContext().getSystemService(
            Context.WINDOW_SERVICE);
    if (winMan != null) {
        int orientation = winMan.getDefaultDisplay().getOrientation();
        if (orientation == 0) {
            // Portrait
            setContentView(R.layout.cameradetails);
        } else if (orientation == 1) {
            // Landscape
            setContentView(R.layout.cameradetailsl);
        }
    }

    Bundle b = getIntent().getExtras();

    ImageButton b1 = (ImageButton) findViewById(R.id.btnNext);
    b1.setOnClickListener(this);
    ImageButton b2 = (ImageButton) findViewById(R.id.btnMain);
    b2.setOnClickListener(this);
    ImageButton b3 = (ImageButton) findViewById(R.id.btnPrevious);
    b3.setOnClickListener(this);
    ImageButton b4 = (ImageButton) findViewById(R.id.btnCam);
    b4.setOnClickListener(this);

    ImageButton b5 = (ImageButton) findViewById(R.id.btnVideo);
    b5.setOnClickListener(this);

    id = Integer.valueOf(b.get("id").toString());
    item = listarrayadapter.cameraList.get(Integer.valueOf(id));
    mv = (MjpegView) findViewById(R.id.mv);
    try {
        getVal(item.get("cameraLink"));
        // getVal("http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg 
                 /video.cgi?resolution=800x600&amp%3bdummy=1333689998337");
    } catch (Exception e) {
        e.printStackTrace();
        mv.setBackgroundResource(R.drawable.offline);
    }

}

@Override
protected void onResume() {

    // if(recording)
    // {
    // getVal("http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg
             /video.cgi?resolution=800x600&amp%3bdummy=1333689998337");
    // }

    super.onResume();
}

public void onPause() {
    super.onPause();
    dialog.dismiss();
    mv.stopPlayback();
}

private void getVal(final String url) {
    Log.i("URL===", url);

    updateButtons();
    dialog = ProgressDialog.show(this, null, null, true);
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {

            dialog.dismiss();

        }
    };

    Thread checkUpdate = null;
    checkUpdate = new Thread() {
        public void run() {
            mv.setSource(MjpegInputStream.read(url));
            mv.setDisplayMode(MjpegView.SIZE_FULLSCREEN);
            handler.sendEmptyMessage(0);
        }
    };
    checkUpdate.start();

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    int btn = v.getId();

    if (btn == R.id.btnMain) {
        Intent intent = new Intent();
        intent.setClass(CameraDetails2.this, CamerasList.class);
        startActivity(intent);
        finish();
    }
    if (btn == R.id.btnNext) {

        id += 1;
        Intent myintent = new Intent(CameraDetails2.this,
                CameraDetails.class);

        myintent.putExtra("id", id);
        startActivity(myintent);
        finish();

    }
    if (btn == R.id.btnPrevious) {
        id -= 1;
        Intent myintent = new Intent(CameraDetails2.this,
                CameraDetails.class);
        myintent.putExtra("id", id);
        startActivity(myintent);
        finish();

    }
    if (btn == R.id.btnCam) {
        if (mv != null) {
            Date dt = new Date();
            int years = dt.getYear();
            int month = dt.getMonth();
            int day = dt.getDay();
            int hours = dt.getHours();
            int minutes = dt.getMinutes();
            int seconds = dt.getSeconds();
            final String filename = years + "" + month + "" + day + ""
                    + hours + "" + minutes + "" + seconds;

            try {

                Bitmap image = MjpegView.savebmp;

                File SDCardRoot = 
                       Environment.getExternalStorageDirectory();
                FileOutputStream fileOutputStream = null;
                fileOutputStream = new FileOutputStream(
                        SDCardRoot.toString() + "/" + 
                             filename + ".jpg");
                BufferedOutputStream bos = new 
                     BufferedOutputStream(
                        fileOutputStream);
                int quality = 95;
                image.compress(CompressFormat.JPEG, quality, bos);
                final String szFile = SDCardRoot.toString() + "/"
                        + filename + ".jpg";

                m_pScanner = new MediaScannerConnection(this,
                        new MediaScannerConnectionClient() 
           {
                            public void 
            onMediaScannerConnected() {
                                m_pScanner

                      .scanFile(szFile, null /* mimeType */);
                            }

                            public void 
       onScanCompleted(String path, Uri uri) {
                                if 
        (path.equals(szFile)) {

            CameraDetails2.this

        .runOnUiThread(new Runnable() {
                                                public void run() {
                                                    Toast.makeText(
                                                            getApplicationContext(),
                                                            "Image Saved.",
                                                            Toast.LENGTH_LONG)
                                                            .show();

                                                }
                                            });

                          m_pScanner.disconnect();
                                }
                            }

                        });
                m_pScanner.connect();

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    if (btn == R.id.btnVideo) {
        if (recording) {

            // stop and save

            recording = false;
            MjpegInputStream.isrecording = false;
            List<byte[]> bytelist = new ArrayList<byte[]>();
            ArrayList<ByteArrayOutputStream> byteArrayStream =
                            MjpegInputStream.byteArrayStream;
            for (int i = 0; i < byteArrayStream.size(); i++) {

                byte[] templist      
                                   =byteArrayStream.get(i).toByteArray();
                bytelist.add(templist);

            }

            for (int j = 0; j < bytelist.size(); j++) {
                bytearraysize += bytelist.get(j).length;
            }

            byte[] totalbytes = new byte[bytearraysize];
            int f = 0;

            for (int j = 0; j < bytelist.size(); j++) {
                for (int a = 0; a < bytelist.get(j).length; a++) {
                    totalbytes[f] = bytelist.get(j)[a];
                    f++;
                }
            }

            Log.e("num of bytes", "" + totalbytes.length);

            // Byte[] bytes = bytelist.toArray(new 
                                     //Byte[bytelist.size()]);

            try {
                writeToFile(totalbytes, "" + 
                  System.currentTimeMillis());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            recording = true;
            MjpegInputStream.isrecording = true;
            // onResume();
            // start recording
        }

        // recorder=new MediaRecorder();
        // try {
        // recorder.prepare();
        // } catch (IllegalStateException e) {
        // e.printStackTrace();
        // finish();
        // } catch (IOException e) {
        // e.printStackTrace();
        // finish();
        // }
        //
        // //recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        // // recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        //
        // //
        // //
        // // CamcorderProfile cpHigh = CamcorderProfile
        // // .get(CamcorderProfile.QUALITY_HIGH);
        // // recorder.setProfile(cpHigh);
        // recorder.setVideoSource(mv.getId());
        // recorder.setOutputFile("/sdcard/videocapture_example.mp4");
        // recorder.setMaxDuration(50000); // 50 seconds
        // recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
    }

}

public void writeToFile(byte[] bytes, String videoname) throws IOException {
    try {
        Log.e("num of bytes to be saved", "" + bytes.length);
        String path = "/sdcard/" + videoname + ".mp4";
        FileOutputStream stream = new FileOutputStream(path);
        stream.write(bytes);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

private void updateButtons() {
    ImageButton btnNext = (ImageButton) findViewById(R.id.btnNext);
    ImageButton btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
    if (id == 0) {
        btnPrevious.setEnabled(false);
    } else {
        btnPrevious.setEnabled(true);
    }
    if (id == listarrayadapter.cameraList.size() - 1) {
        btnNext.setEnabled(false);
    } else {
        btnNext.setEnabled(true);
    }
}

    }
4

0 回答 0