How to convert Mat data to byte data in Andorid by using OpenCV

asked 2016-12-31 05:30:07 -0500

newbie_Opencv gravatar image

updated 2016-12-31 05:44:52 -0500

I'd like to convert Mat data to byte data by using OpencV.

My expected data format is the following:

ex.) data =[112,113,0,1,0,225,........] →1,0,225 is one layer CV_8UC3

but my code(System.outprint.ln(data)) is not equal to the above data format. The Output value is "I/System.out: [B@43d1d008".

How should I achieve the problem?

My code: ・when I touch the screen, the Flag is to be set as "1"; then , in onCameraFrame, the Mat date is converted to byte data and execute "System.outprint.ln" .

package opencv.ros.com.camera_opencv;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.WindowManager;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;

import java.io.BufferedOutputStream;



public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener {
    private CameraBridgeViewBase mCameraView;
    private Mat mOutputFrame;

    private volatile int Flag=0;


    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    mCameraView.enableView();
                    break;
                default:
                    super.onManagerConnected(status);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mCameraView = (CameraBridgeViewBase)findViewById(R.id.camera_view);
        mCameraView.setCvCameraViewListener(this);
    }

    @Override
    public void onPause() {
        if (mCameraView != null) {
            mCameraView.disableView();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_4, this, mLoaderCallback);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
            Flag = 1;
            System.out.println( Flag );
        }
        return true;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCameraView != null) {
            mCameraView.disableView();
        }
    }

    @Override
    public void onCameraViewStarted(int width, int height) {
        // Mat(int rows, int cols, int type)
        // rows: height, cols: width
        mOutputFrame = new Mat(height, width, CvType.CV_8UC3);
    }

    @Override
    public void onCameraViewStopped() {
        mOutputFrame.release();
    }

    @Override
    public Mat onCameraFrame(Mat inputFrame) {
        Mat img2 = new Mat(new Size(85,85),CvType.CV_8UC3);

        if(Flag==1){
            Flag=0;
            int bufferSize = img2.channels()*img2.cols()*img2.rows();
            byte [] b = new byte[bufferSize];
            img2.get(0,0,b); // get all the pixels

            System.out.println(b);

        }
        return img2;
    }



}
edit retag flag offensive close merge delete

Comments

  • why do you need the bytes ?
  • did you want: System.out.println(img2.dump()); ?
berak gravatar imageberak ( 2016-12-31 05:48:09 -0500 )edit

Because I 'd like to send the byte date via TCP communication. So, I'd like to convert Mat data to the byte array.

newbie_Opencv gravatar imagenewbie_Opencv ( 2016-12-31 05:53:19 -0500 )edit

ok.(though, imencode() to jpg would be a better idea, then sending raw pixels)

but again, there's nothing wrong with the printout. (it's the address of the byte[])

if you want to see the individual bytes, you have to write a loop for that

berak gravatar imageberak ( 2016-12-31 05:58:35 -0500 )edit

Thank you.

You just said , by using"System.out.println(img2.dump());" ,I can printout the data that I expect as the following.
ex.) data =[112,113,0,1,0,225,........]

But , I'd like to convert the Mat data to byte data.

newbie_Opencv gravatar imagenewbie_Opencv ( 2016-12-31 06:15:41 -0500 )edit