I'm trying to do something like this:
Android Map api v2 Custom marker with ImageView
But I'm stuck on using image loader.
Trying:
Bitmap bmImg = imageLoader.loadImageSync(url);
LogCat gives me
04-13 14:11:44.953: E/ImageLoader(18542): android.os.NetworkOnMainThreadException
Here's is my code. I have an ArrayList camera with all information needed (titrle, position, url, etc).
public void drawPicsOnMap()
{
String title = null;
String place = null;
for (int i = 0; i<camera.size(); i++)
{
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
Canvas canvas = new Canvas(bmp);
// paint defines the text color,
// stroke width, size
Paint color = new Paint();
color.setTextSize(35);
color.setColor(Color.BLACK);
Camera cam = camera.get(i);
LatLng coord = new LatLng(cam.lat, cam.lon);
title = Integer.toString(cam.id);
place = cam.place;
String url = cam.img;
Bitmap bmImg = imageLoader.loadImageSync(url);
//modify canvas
canvas.drawBitmap(bmImg, 0,0, color);
canvas.drawText(title, 30, 40, color);
Marker mark = map.addMarker(new MarkerOptions()
.position(coord)
.title(title)
.snippet(place)
.icon(BitmapDescriptorFactory
.fromBitmap(bmp)));
markers.put(mark.getId(), url);
}
}
Try this:
imageLoader.loadImage(url, new SimpleImageLoadingListener(){
#Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
//write your code here to use loadedImage
}
});
Here
onLoadingComplete will be called on UI thread which makes it thread safe
You can not do any networking on the main thread. See for description. UIL usually creates the thread and stuff for you when you call displayImage, but loadImageSync does not. Either create a thread and load it or use an AsynTask.
Related
I am creating an app that pulls images from urls and puts them into a recyclerview. The user can then access those images and view it fullscreen. This is achieved with Picasso. I would now like the ability to fingerpaint over the image loaded with Picasso with an onTouchEvent or something but not sure how to do it.
This class sets the image to a map_edit_gallery.xml loaded with Picasso:
public class EditMapImage extends AppCompatActivity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_edit_gallery);
checkIntent();
//Find savebutton
ImageButton saveMapButton = findViewById(R.id.saveEditImagebutton);
saveMapButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_SHORT).show();
}
});
}
//This will check to see if the intent extras exist and if they do get the extra
private void checkIntent(){
if(getIntent().hasExtra("image_url") && getIntent().hasExtra("name_url")){
String imageUrl = getIntent().getStringExtra("image_url");
String nameUrl = getIntent().getStringExtra("name_url");
setMapImage(imageUrl, nameUrl);
}
}
private void setMapImage(String imageUrl, String nameUrl){
//Set the Text view
TextView name = findViewById(R.id.mapNameEditor);
name.setText(nameUrl);
//Set the Image
ImageView imageView = findViewById(R.id.mapEditScreen);
Picasso.get().load(imageUrl).into(imageView);
Picasso picasso = Picasso.get();
DrawToImage myTransformation = new DrawToImage();
picasso.load(imageUrl).transform(myTransformation).into(imageView);
}
}
EDIT:
This class has allowed me to draw over the loaded image using canvas but cannot figure out how to use touch to draw:
public class DrawToImage implements Transformation {
#Override
public String key() {
// TODO Auto-generated method stub
return "drawline";
}
public Bitmap transform(Bitmap bitmap) {
// TODO Auto-generated method stub
synchronized (DrawToImage.class) {
if(bitmap == null) {
return null;
}
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
canvas.drawLine(0, resultBitmap.getHeight()/2, resultBitmap.getWidth(), resultBitmap.getHeight()/2, paint);
bitmap.recycle();
return resultBitmap;
}
}
}
Try using the image selected by the user to set it in a canvas object and draw on the canvas object itself, as opposed to the image. There are plenty of tutorials out there to help you with how to draw on a canvas.
This process isn't connected with the Picasso Image Library in any way so I would recommend first getting the image through Picasso, then sending the image into your custom canvas implementation, then returning a bitmap/drawable which you could set into Picasso after editing.
There's also plenty of tutorials on how to export an image from the canvas to get your edited image when you need it.
I hope this helped, Panos.
This is an Android app to convert an RGB image to grayscale and display it on a screen. According to logcat, I am getting an unsatisfiedLinkError from
Mat ImageMat = new Mat (image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));
What is wrong?
public class ImwriteActivity extends Activity /*implements OnClickListener*/{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imwrite);
ImageView img = (ImageView) findViewById(R.id.imageView1);
//get image from sdcard
File f = new File(Environment.getExternalStorageDirectory().getPath()+"/Test.jpg");
Bitmap image = BitmapFactory.decodeFile(f.getAbsolutePath());
//convert Bitmap to Mat
Mat ImageMat = new Mat (image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));
Bitmap myBitmap32 = image.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, ImageMat);
//change the color
Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_RGB2GRAY,4);
//convert the processed Mat to Bitmap
Bitmap resultBitmap = Bitmap.createBitmap(ImageMat.cols(), ImageMat.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(ImageMat, resultBitmap);
//Set member to the Result Bitmap. This member is displayed in an ImageView
img.setImageBitmap(resultBitmap);
}
}
onCreate is the wrong place to put your opencv code, since it depends on native, c++ code.
first you need to load the opencv so's:
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
if (status == LoaderCallbackInterface.SUCCESS ) {
// now we can call opencv code !
} else {
super.onManagerConnected(status);
}
}
};
#Override
public void onResume() {;
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
// you may be tempted, to do something here, but it's *async*, and may take some time,
// so any opencv call here will lead to unresolved native errors.
}
#Override
public void onCameraViewStarted(int width, int height) {
// probably the best place for your opencv code
}
For example ..
Bitmap myPic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap newPic = Bitmap.createScaledBitmap(myPic, 50, 50, true);
Canvas myCanv = new Canvas(newPic);
View myView = (View)findViewById(R.id.view1);
myView.draw(myCanv);
is something like the above feasible?
You really shouldn't do it like that. My recommendation is to do it like this
Bitmap myPic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
myPic = Bitmap.createScaledBitmap(myPic,50,50, true);
BitmapDrawable draw = new BitmapDrawable(this.getResources(), myPic);
View myView = (View) findViewById (R.id.view1);
myView.setBackground(draw);
This way you pass your View a drawable as a background.
If you really want to set a Bitmap as your source then you should make an ImageView
I am getting the current wallpaper by using following code:
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
How can I create a bitmap from this?
like when I create a bitmap from res folder I use this
Resources res = getApplicationContext().getResources();
b = BitmapFactory.decodeResource(res, R.drawable.wall);
What code I should use to get current wallpaper into the bitmap so I can draw it on my canvas and use it as my live wallpaper background?
The Drawable fetched should really be a BitmapDrawable. You can verify this using instanceof if necessary.
That being the case, all you have to do is:
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final Bitmap wallpaperBitmap = ((BitmapDrawable) wallpaperDrawable).getBitmap();
EDIT: If it turns out that the Drawable is NOT a BitmapDrawable, you can use the following method to convert it (found in this answer, credit to André):
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
I have a thread that call a function in my main activity that should add an image to my layout at a certain position.. When i call my function all works properly but my layout's parameter (margin in my case) resets when i call:
myLayout.addView(newImage);
That's my function that my thread calls:
public void createImageAt(Point location) {
final ImageView newImage = new ImageView(this);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(90, 90);
params.leftMargin = location.x;
params.topMargin = location.y;
newImage.setImageResource(R.drawable.image);
newImage.setLayoutParams(params);
final RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.gameLayout);
myLayout.post(new Runnable() {
#Override
public void run() {
myLayout.addView(newImage);
}
});
}
My problem is that i have to slide my layout by moving the leftMargin, and when i call the function wrote above the margin jump back to 0 even if it was at -200px
If the problem, that the state does not saves, make newImage global variable and reuse it. Just use myLayout.addView(newImage); and myLayout.remove(newImage);