How to display an `Image` in the `ImageView`? - android

I have an ImageView object acquired from the .xml file:
mCameraView = (ImageView) findViewById(R.id.camera_view);
I record an image taken from the camera many times a second. For each new image frame the following method gets called.
#Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
mCameraView.somehowDisplay(image); // HOW?
}
I want place the image into the image view: How to do that?

ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
mCameraView.setImageBitmap(myBitmap);
This should work.
Reference Answer

Related

Not able to get actual uri of image clicked from camera

After implementing Camera2 Api for clicking image I am getting image in this listener
private ImageReader.OnImageAvailableListener imageAvailableListener = new ImageReader.OnImageAvailableListener() {
#Override
public void onImageAvailable(ImageReader reader) {
Image img = reader.acquireLatestImage();
final Image.Plane[] planes = img.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
byte[] byteArray = new byte[buffer.remaining()];
buffer.get(byteArray);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
uri = getImageUri(getContext(), bmp);
}
};
I tried to parse the Image to Bitmap in order to get the image uri using this method
private Uri getImageUri(Context context, Bitmap inImage) {
this.saveToInternalStorage(inImage);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "CAPTURE" + captureCount, null);
return Uri.parse(path);
}
But the image uri, which I get from above method, when I try to use it for displaying image in ImageView doesnot shows up any thing but I can see the clicked image in gallery.
Image uri which I get starts like external/**/<some number>
What I am trying to do is I want to get uri of clicked image and use
that uri to edit it.
If there is any other way to edit image may be like using bitmap or some other form pls do mention.

Android Bitmap won't compress

I'm trying to compress a Bitmap which is taken from either the user's gallery or camera and store it as a profile picture in a Parse Server.
The issue is the bitmap will NOT compress. The image saves perfectly fine and is useable in the database, but the file size is massive for just a profile picture.
Here's my current code:
//Compressing
ByteArrayOutputStream stream = new ByteArrayOutputStream();
profilePictureBitmap.compress(Bitmap.CompressFormat.PNG, 20, stream);
byte[] image = stream.toByteArray();
//Saving
String imageName = username + "_profile_picture.png";
final ParseFile file = new ParseFile(imageName, image);
file.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if(e == null) {
user.put("profilePicture", file);
user.signUpInBackground();
}
}
}
I'm using a image picker library that gets the path of the image. I then turn it into a bitmap.
Heres my code to retrieve the image:
ArrayList<Image> images = data.getParcelableArrayListExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES);
if(images.size() > 0) {
Image image = images.get(0);
File imgFile = new File(image.getPath());
if(imgFile.exists()){
profilePictureBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
profilePictureImage.setImageBitmap(profilePictureBitmap);
}
}
If there is any ideas on how to fix this I would greatly appreciate it.
Thanks :]
Image image = images.get(0);
File imgFile = new File(image.getPath());
if(imgFile.exists()){
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2; // one quarter of original size
profilePictureBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), opts);
profilePictureImage.setImageBitmap(profilePictureBitmap);
}
Docs for inSampleSize:
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.

camera2 API captured image preview

The function onImageAvailable handles the image save, how can it be used to set a preview for the user to review, typically on an ImageView.
#Override
public void onImageAvailable(ImageReader reader) {
//mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile));
Image image = reader.acquireNextImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
setPreviewImage(bmp);
}
This does not work for me.

How to convert an Image to Mat Android

I need to process Image for my Application. I get the Images for ImageReader.
reader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
#Override
public void onImageAvailable(ImageReader reader) {
Image mImage = reader.acquireNextImage();
//mImage to Mat here
mImage.close();
}
},null);
But now, I need to convert those images in Mat.
I know that I can pass by the Bitmap class, but i don't know how to convert an Image into Bitmap too.
I think that i found a possible answer.
I give to my ImageReader a simple plane format like JPEG.
reader = ImageReader.newInstance(previewSize.getWidth(),previewSize.getHeight(), ImageFormat.JPEG, 2);
Then i do that :
ByteBuffer bb = image.getPlanes()[0].getBuffer();
byte[] buf = new byte[bb.remaining()];
bb.get(buf);
imageGrab = new Mat();
imageGrab.put(0,0,buf);
Try this
// image to byte array
ByteBuffer bb = image.getPlanes()[0].getBuffer();
byte[] data = new byte[bb.remaining()];
bb.get(data);
// byte array to mat
Mat = Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

Bytearray to string android

I am trying to create a camera intent and a sub portion of the code is given below.
public void onPictureTaken(byte[] data, Camera camera) {
String dat = new String(data);
byte[] datas = dat.getBytes();
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
Bitmap bMap = BitmapFactory.decodeByteArray(datas, 0, datas.length);
iv2.setImageBitmap(bMap);
}
This keeps the imageview blank, however when I give
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
the imageview is properly loaded. Am I doing any mistake in byte array to string conversion??
why do you need these two lines?
String dat = new String(data);
byte[] datas = dat.getBytes();
use data directly in the decodeByteArray
You dont even need to convert the byte[] to String. Just use it as it is.
public void onPictureTaken(byte[] data, Camera camera) {
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
// ensure ImageView is visible.
iv2.setVisibility( View.VISIBLE);
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
iv2.setImageBitmap(bMap);
}
Just use the above modified code.

Categories

Resources