2

I have created an app and using nanohttpd I could get info from the phone. I could open camera in the background and took the picture. But I cannot stream it as motion jpeg through nanohttpd.

I have searched the answer on the Internet, but unfortunately, I could not reach any success. I have found many ways and open source projects(ipweb-cam, spydroid), but they didn't help me.

Please, help me.

Sherzodbek
  • 296
  • 3
  • 9
  • Yes, I found MJpegStreamer class on the Internet, and only thing I had to do was using this class send every frame of camera to some url continuously, if you need any additional information feel free to write to me! – Sherzodbek Jun 17 '17 at 09:40
  • Hi, can you share deeper how is the solution you found on Intenet?. I'm making something similar you purpose here and I dont know to serve stream jpeg through nanohttpd server. Thanks in advance! – Jaime Menendez Llana Jul 20 '17 at 11:13
  • Firstly, you need to open your camera inside your nanohttpd server, as long as you get preview frames you need to use the MJpegStream class to stream those frames. I will give my own code (class) if you want because I made it simpler and easier to use. – Sherzodbek Jul 21 '17 at 06:53
  • Hi, I took the MJpegStreamer class, but my problem is how to take picture continuosly to send the preview frame to the webserver. – Jaime Menendez Llana Jul 31 '17 at 10:18

1 Answers1

0

For those who are interested in the answer, and need some code, here:

cameraObject.setPreviewCallback(new Camera.PreviewCallback(){
     @Override
     public void onPreviewFrame(byte[] data, Camera camera) {

            try {

                if (cameraObject.getParameters().getPreviewFormat() == ImageFormat.NV21) {
                    timestampBeforecompression = SystemClock.uptimeMillis();
                    yuvImage = new YuvImage(data, imageFormat, width, height, null);
                    yuvImage.compressToJpeg(rect, 75, mByteArrayOutputStream);

                    frameToStream = mByteArrayOutputStream.toByteArray();

                    bitmap = BitmapFactory.decodeByteArray(frameToStream, 0, frameToStream.length);
                    mByteArrayOutputStream.reset();
                    bitmap = Bitmap.createScaledBitmap(bitmap, 320, 240, true);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 60, mByteArrayOutputStream);
                    frameToStream = null;
                    frameToStream = mByteArrayOutputStream.toByteArray();

                    compressionMillis = SystemClock.uptimeMillis() - timestampBeforecompression;
                    if (125 - compressionMillis > 0) {
                        SystemClock.sleep(125 - compressionMillis);
                    }

                    if (mJpegStream != null) {
                        mJpegStream.streamJpeg(frameToStream, frameToStream.length, Long.MIN_VALUE);
                    }

                    if (mByteArrayOutputStream != null) {
                        mByteArrayOutputStream.reset();
                    }

                    frameToStream = null;
               }

           } catch (Exception e) {
                System.out.println("Oops: " + e.getMessage());
           }

      }
});

Note that you can omit compressionMillis and System.sleep(). I used them to control frame rate, if you have any misunderstanding feel free to write.

Sherzodbek
  • 296
  • 3
  • 9