0

I want to convert the following code to java language. however I can't find the way of doing reinterpret_cast in java. please help me.

char *pixels= reinterpret_cast<char*>(FinalImage.data);

the program can be shown as follows.I want to detect blur image.

Mat newEx=new Mat();
            final int MEDIAN_BLUR_FILTER_SIZE=15;
            Imgproc.medianBlur(mat1, newEx, MEDIAN_BLUR_FILTER_SIZE);

            Mat LaplacianImage= new Mat();
            Imgproc.Laplacian(newEx, LaplacianImage,CvType.CV_8U);

            Mat LaplacianImage8Bit=new Mat();
            LaplacianImage8Bit.convertTo(LaplacianImage8Bit,CvType.CV_8UC1);
            Mat FinalImage=new Mat();
            Imgproc.cvtColor(LaplacianImage8Bit,FinalImage,Imgproc.COLOR_BGR2BGRA);

            int rows= FinalImage.rows();
            int cols= FinalImage.cols();

            char *pixels= reinterpret_cast<char*>(FinalImage.data);
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 10
    There isn't, to my knowledge, an equivalent in Java. Casting is only allowed between compatible types. Instead of positing C++ code and asking for direct translation; please post the Java you have so far and explain where you are stuck - it is almost impossible to translate arbitrary, idiomatic, code from one language to another... – Boris the Spider Mar 24 '16 at 08:22
  • If I understand correctly, you are trying to transform an image into an array of chars? Maybe try something like this? String imageDataString = Base64.encodeBase64URLSafeString(imageByteArray) – Manuel S. Mar 24 '16 at 08:28
  • 1
    Post your java code and describe what you are trying. Like converting byte[] to Base64 encoded String? – tak3shi Mar 24 '16 at 08:30
  • What is the `Mat` class you are using here? Would it be `org.opencv.core.Mat`? – Serge Ballesta Mar 24 '16 at 10:42

2 Answers2

0

I understand that you have an object, FinalImage.data, and you want to cast the data stored in that object to a string.

That's exactly what the Serializable interface does. So if your FinalImage.data object inherits that you could use code something like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(FinalImage.data);
String pixels = baos.toString();
oos.close();
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0

Java is a higher level language than C or C++ and has no direct way to process a memory zone as a char array.

But here you are using OpenCV classes, and the Java OpenCV library offers some tools, because it knows that a matrix of bytes in internally a byte array. And same for common element sizes.

Unfortunately, I am not used enough to opencv to guess what is the internal type of FinalImage, but I think that it should be a MatOfByte as you use CV_8U and CV_8UC1 types. If it is you could try this:

MatOfByte byteMat = (MatOfByte) FinalImage;
byte[] internalBytes = byteMat.toArray();
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252