Android Camera: How to process byte data in onPictureTaken using openCV
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to process an image that comes from a JpegImageCallback
like this
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mContentView.getCamera().setPreviewCallback(null);
mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
}
});
The JpegImageCallback
looks like this
class JpegImageCallback implements Camera.PictureCallback {
@Override
public void onPictureTaken(byte data, Camera camera) {
Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);
Camera.Parameters params = camera.getParameters();
int fw = params.getPictureSize().width;
int fh = params.getPictureSize().height;
Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */
/* conversion data to Mat */
Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
mYuvFrameData.put(0, 0, data);
Mat rgbImage = new Mat();
Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
}
}
The conversion from data to org.opencv.core.Mat
is copied from org.opencv.android.JavaCameraView
. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera)
in a Camera.PreviewCallback
. But in a Camera.PictureCallback
like above I get the image wrong.
After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).
I already changed the image format to be the same in preview and picture (ImageFormat.NV21
), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG
if I don't change it.
So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken
like in onPreviewFrame
?
Thanks for any ideas!
Edit:
I dont't want to save the image to disk and I don't want to convert it to a bitmap.
android opencv
add a comment |
I'm trying to process an image that comes from a JpegImageCallback
like this
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mContentView.getCamera().setPreviewCallback(null);
mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
}
});
The JpegImageCallback
looks like this
class JpegImageCallback implements Camera.PictureCallback {
@Override
public void onPictureTaken(byte data, Camera camera) {
Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);
Camera.Parameters params = camera.getParameters();
int fw = params.getPictureSize().width;
int fh = params.getPictureSize().height;
Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */
/* conversion data to Mat */
Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
mYuvFrameData.put(0, 0, data);
Mat rgbImage = new Mat();
Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
}
}
The conversion from data to org.opencv.core.Mat
is copied from org.opencv.android.JavaCameraView
. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera)
in a Camera.PreviewCallback
. But in a Camera.PictureCallback
like above I get the image wrong.
After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).
I already changed the image format to be the same in preview and picture (ImageFormat.NV21
), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG
if I don't change it.
So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken
like in onPreviewFrame
?
Thanks for any ideas!
Edit:
I dont't want to save the image to disk and I don't want to convert it to a bitmap.
android opencv
add a comment |
I'm trying to process an image that comes from a JpegImageCallback
like this
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mContentView.getCamera().setPreviewCallback(null);
mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
}
});
The JpegImageCallback
looks like this
class JpegImageCallback implements Camera.PictureCallback {
@Override
public void onPictureTaken(byte data, Camera camera) {
Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);
Camera.Parameters params = camera.getParameters();
int fw = params.getPictureSize().width;
int fh = params.getPictureSize().height;
Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */
/* conversion data to Mat */
Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
mYuvFrameData.put(0, 0, data);
Mat rgbImage = new Mat();
Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
}
}
The conversion from data to org.opencv.core.Mat
is copied from org.opencv.android.JavaCameraView
. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera)
in a Camera.PreviewCallback
. But in a Camera.PictureCallback
like above I get the image wrong.
After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).
I already changed the image format to be the same in preview and picture (ImageFormat.NV21
), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG
if I don't change it.
So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken
like in onPreviewFrame
?
Thanks for any ideas!
Edit:
I dont't want to save the image to disk and I don't want to convert it to a bitmap.
android opencv
I'm trying to process an image that comes from a JpegImageCallback
like this
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mContentView.getCamera().setPreviewCallback(null);
mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
}
});
The JpegImageCallback
looks like this
class JpegImageCallback implements Camera.PictureCallback {
@Override
public void onPictureTaken(byte data, Camera camera) {
Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);
Camera.Parameters params = camera.getParameters();
int fw = params.getPictureSize().width;
int fh = params.getPictureSize().height;
Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */
/* conversion data to Mat */
Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
mYuvFrameData.put(0, 0, data);
Mat rgbImage = new Mat();
Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
}
}
The conversion from data to org.opencv.core.Mat
is copied from org.opencv.android.JavaCameraView
. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera)
in a Camera.PreviewCallback
. But in a Camera.PictureCallback
like above I get the image wrong.
After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).
I already changed the image format to be the same in preview and picture (ImageFormat.NV21
), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG
if I don't change it.
So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken
like in onPreviewFrame
?
Thanks for any ideas!
Edit:
I dont't want to save the image to disk and I don't want to convert it to a bitmap.
android opencv
android opencv
edited Nov 23 '18 at 14:31
Julia Kurde
asked Nov 23 '18 at 13:29
Julia KurdeJulia Kurde
286
286
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This works:
Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
mat.put(0, 0, data);
Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat gryImage = new Mat();
Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);
Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?
Imgcodecs.imdecode
does all the magic!
There is no need to change the image format, by the way.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447626%2fandroid-camera-how-to-process-byte-data-in-onpicturetaken-using-opencv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This works:
Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
mat.put(0, 0, data);
Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat gryImage = new Mat();
Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);
Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?
Imgcodecs.imdecode
does all the magic!
There is no need to change the image format, by the way.
add a comment |
This works:
Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
mat.put(0, 0, data);
Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat gryImage = new Mat();
Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);
Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?
Imgcodecs.imdecode
does all the magic!
There is no need to change the image format, by the way.
add a comment |
This works:
Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
mat.put(0, 0, data);
Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat gryImage = new Mat();
Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);
Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?
Imgcodecs.imdecode
does all the magic!
There is no need to change the image format, by the way.
This works:
Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
mat.put(0, 0, data);
Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat gryImage = new Mat();
Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);
Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?
Imgcodecs.imdecode
does all the magic!
There is no need to change the image format, by the way.
answered Nov 26 '18 at 13:29
Julia KurdeJulia Kurde
286
286
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447626%2fandroid-camera-how-to-process-byte-data-in-onpicturetaken-using-opencv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown