I am using:
https://github.com/jasonpolites/gesture-imageview
on load of the app, it has a placeholder image in a GestureImageView that pinch/zooms appropriately. I have a button that when clicks fires a camera intent, saves the file, and then I wish to set that image to be the source bitmap used in the gestureimageview.
GestureImageView imageView = (GestureImageView) findViewById(R.id.imageViewOne);
ContentResolver cr = getContentResolver();
getContentResolver().notifyChange(imageUriOne, null);
try {
Bitmap mybitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUriOne);
imageView.setImageBitmap(mybitmap);
}
For a normal imageview, that works. But for the GestureImageView, the image stays as the original once returned from the camera intent, and if touched disappears.
To check it's not the bitmap that's the problem, I tried
int idTwo=getResources().getIdentifier("com.jazz.test1:drawable/second_photo", null, null);
imageView.setImageResource(idTwo);
I.e. set the imageview to an existing resource, but this has the same problem.
If I call that setImageResource code before the intent, it does work.
Any ideas how to debug? There are no errors in the logs.
Resolution is here:
https://github.com/jasonpolites/gesture-imageview/issues/21
hadn't noticed it when I initially looked at the github issues.
You have to replace your initMethod function. With this code it will work properly (GestureImageView.java file in com.polites.android package).
protected void initImage() {
if (this.drawable != null) {
this.drawable.setAlpha(alpha);
this.drawable.setFilterBitmap(true);
if (colorFilter != null) {
this.drawable.setColorFilter(colorFilter);
}
// Keppel.Cao
layout = false;
startingScale = -1.0f;
}
if (!layout) {
requestLayout();
// Keppel.Cao
// redraw();
reset();
}
}
Like Dave said. More you can find here Issue 21
Related
I am displaying an image in a RecyclerView whose source is is a bitmap taken from an MMS message. The problem is that the image is not displaying. Absolutely nothing is displayed. Here is my onBindView:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = mDataset.get(position).getContact() ;
final MMSMessage message = mDataset.get(position);
holder.txtHeader.setText(name);
DateTime dateTime = new DateTime(message.getDate());
holder.txtDate.setText(dateTime.toString(Globals.generalSQLFormatterDT));
holder.txtText.setText(message.getBody());
holder.txtText.setVisibility(View.VISIBLE);
Bitmap bitmap = message.getBitmap();
if (bitmap != null) {
//bitmap is not null and I can see an image using Android Studio
bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false);
holder.imgMMS.setImageBitmap(bitmap);
} else {
holder.imgMMS.setVisibility(View.GONE);
}
}
The xml for the ImageView:
<ImageView
android:layout_below="#+id/thirdLine"
android:id="#+id/imageMMS"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
/>
I looked here and tried to scale down the image to an arbitrary small size. I don't think it's an out of memory error - I tried putting in the launcher icon as a test. What am I doing wrong?
if (bitmap != null) {
//bitmap is not null and I can see an image using Android Studio
bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false);
holder.imgMMS.setImageBitmap(bitmap);
holder.imgMMS.setVisibility(View.GONE);
} else {
holder.imgMMS.setVisibility(View.GONE);
}
You are setting visibility to GONE. My guess is that the RecyclerView is recycling the views, and when it does the view is GONE since you are not setting it to Visible. Try adding holder.imgMMS.setVisibility(View.VISIBLE); for when bitmap is not null, like so:
if (bitmap != null) {
//bitmap is not null and I can see an image using Android Studio
bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false);
holder.imgMMS.setImageBitmap(bitmap);
holder.imgMMS.setVisibility(View.VISIBLE);
} else {
holder.imgMMS.setVisibility(View.GONE);
}
I wrote test application for capturing the images with MediaProjection class.
imageReader = ImageReader.newInstance(currentWidth, currentHeight, PixelFormat.RGBA_8888, 2);
imageReader.setOnImageAvailableListener(this, null);
virtualDisplay = mediaProjection.createVirtualDisplay("captureDisplay",
currentWidth, currentHeight, DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC|
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR |
DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION, Screen.getDensity(mContext), imageReader.getSurface(), null, null);
// DisplayManager flags are trails only
and in onImageAvailable(ImageReader reader) method
i tried to get the image as follows:
Bitmap bitmap;
Image mImage = null;
try {
mImage = mImageReader .acquireLatestImage();
if (img != null) {
final Image.Plane[] planes = mImage.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mImage.getWidth();
bitmap = Bitmap.createBitmap(mImage.getWidth() + rowPadding/pixelStride, mImage.getHeight(), Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] rawData = lastImageAcquiredRaw = stream.toByteArray();
if (rawData != null) {
Bitmap fullScreen = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
savebitmap(fullScreen,"image",i); //Saving bitmap in storage
}
}
Till now I am OK and I am getting correct Image when my app is in landscape orientation. Problem facing is on orientation change, i am not getting proper images. Some times again on changing back to landscape also not capturing properly.
I gone through ImageReader.java class. Nothing has mentioned like need to handle in orientation changes.
Tried using acquireNextImageNoThrowISE(), acquireNextImage() but no use.
Did any one tried or having possibility to get proper image in orientation?
Please help me in getting proper image.
I know this post is a bit old, but the last couple of days I am strangling with the same thing. I am also using MediaProjection API, VirtualDisplay and ImageReader's surface. The solution I am going to demonstrate is not 100% full-proof, so if anyone has any useful suggestions is more than welcome.
Step one: add an OrientationChangeCallback after you create Virtual Display
OrientationChangeCallback orientationChangeCallback = new OrientationChangeCallback(context);
if (orientationChangeCallback.canDetectOrientation()) {
orientationChangeCallback.enable();
}
Step two: define the orientation callback, which in essence restarts the capture
private class OrientationChangeCallback extends OrientationEventListener {
OrientationChangeCallback(Context context) {
super(context);
}
#Override
public void onOrientationChanged(int orientation) {
final int rotation = mDisplay.getRotation();
if(rotation != mRotation) {
mRotation = rotation;
if (mVirtualDisplay != null) {
mVirtualDisplay.release();
}
if (mImageReader != null) {
mImageReader.setOnImageAvailableListener(null, null);
}
if (!mProjectionStopped) {
createVirtualDisplay();
}
}
}
}
Note that I am holding references to ImageReader and VirtualDisplay, and also have a volatile boolean mRotation to check if the screen actually rotated.
The problem I have is that on some devices (Nexus 5X included) the image can become corrupt after X number or rotations (usually 10-20). If I rotate the device one more time the image is captured correctly. On quick devices (Google Pixel) I cannot replicate the corrupt image issue, so I am guessing it might be a synchronisation issue.
I use CrossWalk to display webpage, it works well. I have a feature to capture the whole content as bitmap to save into SDCard, but I cannot found a solution. Using TextureView can only capture a screen size bitmap, anyone has the same issue? BTW, I can capture the whole content using WebView.draw().
Here's some pseudo code that may help. You can get the content height and width from the webview and then you can leverage webview.draw()...so something like the following.
Bitmap bitmap = Bitmap.createBitmap( webView.getWidth(), webView.getContentHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
Then you can output your bitmap file using bitmap.compress and output it to a file output stream
//fos is a FileOutputStream, you can use something else if needed.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
The only issue I foresee with this is that your bitmap may clip the content horizontally since WebView doesn't have a method to get the content width. Otherwise I think this should work.
I never using crosswalk before, but I'm guessing the Crosswalk WebView is also descendant from android.view.View. If so, then you could use android view drawing cache. It look like this
yourWebView.setDrawingCacheEnabled(true);
Bitmap bmp = yourWebView.getDrawingCache();
Then as Paul said, save it through FileOutputStream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
You can't capture XWalkView image by standard ways as described in answers above because Crosswalk XWalkView works with hardware layer (i.e. SurfaceView or TextureView). But you can use getBitmap() method of TextureView class to get it.
In short, you should enable using of TextureView in Crosswalk before XWalkView will be initialized (in Application subclass for example) with:
XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
And find TextureView in views tree to call getBitmap() on it.
Something like that:
/**
* Returns TextureView which is used in XWalkView
*
* #param group
* #return
*/
private TextureView findXWalkTextureView(ViewGroup group) {
int childCount = group.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = group.getChildAt(i);
if (child instanceof TextureView) {
String parentClassName = child.getParent().getClass().toString();
boolean isRightKindOfParent = (parentClassName.contains("XWalk"));
if (isRightKindOfParent) {
return (TextureView) child;
}
} else if (child instanceof ViewGroup) {
TextureView textureView = findXWalkTextureView((ViewGroup) child);
if (textureView != null) {
return textureView;
}
}
}
return null;
}
/**
* Example of capturing image from XWalkView based on TextureView
*
* #return
*/
public Bitmap captureImage() {
if (mXWalkView != null) {
Bitmap bitmap = null;
boolean isCrosswalk = false;
try {
Class.forName("org.xwalk.core.XWalkView");
isCrosswalk = true;
} catch (Exception e) {
e.printStackTrace();
}
if (isCrosswalk) {
try {
TextureView textureView = findXWalkTextureView(mXWalkView);
bitmap = textureView.getBitmap();
} catch (Exception e) {
e.printStackTrace();
}
} else {
bitmap = Bitmap.createBitmap(mXWalkView.getWidth(), mXWalkView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
mXWalkView.draw(c);
}
return bitmap;
} else {
return null;
}
}
Check this code: https://github.com/gitawego/cordova-screenshot/blob/master/src/android/Screenshot.java for more details.
I'm trying to process images in my app. The problem I'm currently facing is related to orientation of images. The thumbnail of the images selected from camera folder of Android appears 90 degrees rotated. I'm getting the thumbnail as following;
Uri thumbUri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, uri.getLastPathSegment());
if (thumbUri != null){
try {
List<String> parts = uri.getPathSegments();
String lastPart = parts.get(parts.size() - 1);
int index = lastPart.indexOf(":");
if (index != -1)
{
lastPart = lastPart.substring(index+1);
}
long id = Long.parseLong(lastPart);
// get a thumbnail for the image
Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
context.getContentResolver(),
id,
MediaStore.Images.Thumbnails.MINI_KIND,
null
);
if (bitmap != null)
{
return bitmap;
}
}
catch (Exception e)
{
Log.e(LOG_TAG, "Unable to generate thumbnail from thumbnail uri " + e.getMessage(), e);
}
}
Also tried to fix it by reading orientation from ExifInterface as;
ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
But orientation returned is always 0 ORIENTATION_UNDEFINED. Unable to get what I'm doing wrong here.
Got the solution. I used getThumbnail(ContentResolver contentResolver, long id) method from BitmapUtil which reads the metadata of image from cursor and then process accordingly.
Thanks to Jason Fry for this useful utility.
You can do a simple workaround by performing additional rotation to your bitmap, using Matrix. It's perfectly simple.
//we don't need a NullPointerException now, do we?
if (bitmap != null)
{
//ever so simply initialize a Matrix
Matrix matrix = new Matrix();
//tell the Matrix it should rotate everything it's applied to by -90 degreeds
matrix.postRotate(-90);
//create a clone of the bitmap while applying the mentioned Matrix
//for width and height attributes, you must always use old bitmap's width and height
bitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(),
matrix, true);
return bitmap;
}
EDIT:
And if you need to determine which orientation (landscape/portrait) were the photos taken in, and then decide which ones to rotate, you can use a code from this question/answer, with ExifInterface.
I see you've already applied something similar, so maybe you should try this modification with only 1 parameter, as in the linked answer:
exif.getAttribute(ExifInterface.TAG_ORIENTATION);
UPDATE:
OK, so it doesn't work either. But it should, right? That's what the mentioned method does. So I suggest you check out what uri.getPath() actually returns (using a debugger or System.out.println()), and see if it's right. Invalid path bugs happened to me plenty of times, and every time it took a while to find out what's wrong.
String path = uri.getPath();
//breakpoint somewhere below
UPDATE 2:
I've done some research and, apparently, you can't get absolute file path from URI. So I found this solution on StackOverflow.com that gives a super-simple workaround.
Link
I am trying to restore the image from Native memory (using NDK,C/C++) but that returns me an Black Image.
What i am doing ::
1)get the image from Drawable
2)apply the rotation to the image
3)After rotation apply the grayscale effect to the image
4)At the end i am trying to save the grayscale image in SD Card
For all the above steps, i am referring this awesome lib,which have the native method to store and restore the images.
Please note image is being stored in the SD card but when i am trying to see the image,its totally black with no display at all.
My Java Implementation ::
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item_rotate_90:
options.inPreferredConfig = Config.ARGB_8888;
bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_cam,options);
storeBitmap(bitmapOrig);
bitmapOrig.recycle();
rotateBitmap(90,_handler);
tempBmp=getBitmapAndFree();
bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8);
jniConvertToGray(tempBmp,bitmapWip);
if(bitmapWip!=null)
{
try
{
Bitmap b = Bitmap.createBitmap(bitmapWip.getWidth(),bitmapWip.getHeight(),Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmapWip, 0, 0, paint);
storeBitmap(b);
SaveGrayScaledImage(b);
b.recycle();
tempBmp.recycle();
} catch (IOException e) {
e.printStackTrace();
}
ivDisplay.setImageBitmap(bitmapWip);
}
break;
}
}
I have not make any changes in native method(means using the same method as this lib have for storing and restoring the image).
Saving image to SD Card ::
private void SaveGrayScaledImage(Bitmap finalBitmap)throws IOException
{
String imageFileName = "Temp" + "_gray";
File albumF = new File("/mnt/sdcard/","gray_img");
if(!albumF.exists())
{
albumF.mkdirs();
}
// File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX,
// albumF);
File imageF = new File(albumF,imageFileName + ".jpeg");
if (imageF.exists()) {
imageF.delete();
imageF.createNewFile();
}
try {
FileOutputStream out = new FileOutputStream(imageF);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
imageF = null;
}
}
While googling, i found that(may be i am wrong) image which returns for Native Memory have the ALPHA_8 bitmap config,so i convert the config ALPHA_8 t0 ARGB_8888,but the result is same.
Conversion of bitmap from ALPHA_8 to ARGB_8888 ::
Bitmap b = Bitmap.createBitmap(bitmapWip.getWidth(),bitmapWip.getHeight(),Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmapWip, 0, 0, paint);
StoreBimap funcation ::
public void storeBitmap(final Bitmap bitmap)
{
if(_handler!=null)
freeBitmap();
_handler=jniStoreBitmapData(bitmap);
}
I have no clue about where i was wrong. i have checked the lib methods and implmentation again and again to find the issue.
I have spent my many hours on this small issue and it really frustrating me.
Let me know please if you need anything else from my side.
Please help me to resolve this issue.
Many Thanks in Advance....
EDIT ::
bitmapHolder=new JniBitmapHolder();
final Options options=new Options();
BitmapFactory.decodeFile(picPath, options);
options.inJustDecodeBounds=true;
options.inPreferredConfig=Config.ARGB_8888;
prepareForDownsampling(options,192,256);
System.gc();
bmpGrayscale=BitmapFactory.decodeFile(picPath,options);
int width = bmpGrayscale.getWidth();
int height = bmpGrayscale.getHeight();
bitmapHolder.storeBitmap(bmpGrayscale);
bmpGrayscale.recycle();
Bitmap thumbnail = null;
int rotationInDegrees = 0;
if (picPath != null) {
Uri uri = Uri.parse(picPath);
ExifInterface exif = null;
try {
exif = new ExifInterface(uri.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int rotation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
rotationInDegrees = exifToDegrees(rotation);
}
rotationInDegrees = 90;
ByteBuffer _handler =null;
switch(rotationInDegrees)
{
case 90:
bitmapHolder.rotateBitmapCw90();
break;
case 180:
bitmapHolder.rotateBitmap180();
break;
}
Bitmap bitmapWip = Bitmap.createBitmap(width,height,Config.ALPHA_8);
bitmapHolder.bitmapGrayScale(bitmapWip);
if(bitmapWip!=null){
File CurrentFile = saveGrayScaledIamge(bitmapWip,
takePhotoFile);
}
I have followed your suggestion/steps but the result is same,getting black image with no display.
ok I've found multiple problems and tips for improvements:
the first createBitmap is run with width*height on a bitmap that got rotated instead of height*width. this should be as:
rotateBitmap(90,_handler);
tempBmp=getBitmapAndFree();
bitmapWip=Bitmap.createBitmap(bitmapOrig.getHeight(),bitmapOrig.getWidth(),Config.ALPHA_8);
when saving file you don't get the correct path (you use a hardcoded path, and Lint warns about it).
jniConvertToGray doesn't really need to go over arrays and can just use a pointer, as it just runs on a single pixel. you store the bitmap into JNI twice instead of once (just do: store, rotate, grayscale, restore&free).
you don't use the new bitmap after you have finished working on it, so if I call rotation multiple times, it doesn't seem to do anything.
you already have bitmapWip rotated and grayscaled. why do you need to make a new bitmap that has its content in it, do a grayscale on it, and then save it ?
functions should be named with lowercase letter in the beginning of their names.
and finally , the most important thing: you use ALPHA_8 for the image that you show and need to save to file. this configuration has no color. it's a mask. In order to see the problem, you should set a background color to the imageView :
ivDisplay.setBackgroundColor(0xFFff0000);
before choosing the rotation, you see nothing red. after choosing it, everything you think is white, has actually become red. that's because it's transparent...
If in any phase of your development you've succeeded saving the image to a file and thought it's a black image (yet the size is not 0) , try to edit it and put a background behind it. Maybe you got lucky and just got transparent pixels...
Adding the fact that you save the file to a jpg format, which doesn't support transparency, might also contribute to unexpected behaviors.
in order to solve this, you should use the same technique i've used - use a single bitmap all the time. no need to create so many. only one should exist on the java world, and it should support having colors.