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.
Related
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
How to get the image detail (name, width, height, size, etc) which is selected in image view
I Generate ImageViewXML
<ImageView android:id="#+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent"/>
and get a function to put selected image from folder to that ImageView
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
//Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Its all working correctly without bug, when i want to get the image detail, such as name by using String.valueOf(bitmap) but it return with : android.graphics.Bitmap#5280fcb4
How to get that image detail ?
Try this
Bitmap bitmap = ((BitmapDrawable)imageView.getBackground()).getBitmap();
int width = bitmap .getWidth();
int height = bitmap .getHeight();
Size of Image
Bitmap bitmap = ((BitmapDrawable)imageView.getBackground()).getBitmap();
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytestream );
byte[] imageByte = bytestream .toByteArray();
long lengthbmp = imageByte .length;
String path = (String) imageView .getTag();
you can get the path of the image by this getTag() from that should get the name of image
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.
Take a picture with the normal smartphone camera.
Ok I've been Googling this for a while now, and everyone seems to use something like the following:
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 25, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
I use this to check the file size:
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
protected int sizeOf(Bitmap data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
return data.getRowBytes() * data.getHeight();
} else {
return data.getByteCount();
}
}
The Bitmap is not getting any smaller, before and after:
Log.d("image", sizeOf(bm)+"");
Log.d("image", sizeOf(decoded)+"");
Results:
11-05 02:51:52.739: D/image(2558): 20155392
11-05 02:51:52.739: D/image(2558): 20155392
Pointers?
Answer:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 50;
Bitmap bmpSample = BitmapFactory.decodeFile(fileUri.getPath(), options);
Log.d("image", sizeOf(bmpPic)+"");
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmSample.compress(Bitmap.CompressFormat.JPEG, 1, out);
byte[] byteArray = out.toByteArray();
Log.d("image", byteArray.length/1024+"");
The compress method, as mentioned in the documentation:
Write a compressed version of the bitmap to the specified outputstream. The bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream()
Thus the variable out now contains the compressed bitmap. Since you check the size after calling decodeStream the bitmap is decompressed and returned to you. Therefore the size is same.
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.