I'm trying to take a screenshot using a background service. This service is like a Facebook chathead, but I want it to take an screenshot when I do a click.
I've developed some code but it doesn't work. The last I've tried was:
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/capture/" + now + ".jpg";
// create bitmap screen capture
Bitmap bitmap;
View v1 = chatHead.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
File imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But is taking an screenshot to my button not to the screen.
I know the problem is here:
View v1 = chatHead.getRootView();
But I don't know how to fix it. Can anyone help me?
I'm actually using Android Studio 2.2.2 and Android 4.0 or greater.
For Lollipop and above you can use the MediaProjection API of Google to take the screenshot but you need to ask for the permission from the user.
You can find the sample screen capture code using MediaProjection Here
For the devices less then Lollipop you need root permission for it.
To get a screenshot containing views not belonging to your app you'll need to use the MediaProjectionManager.
See How to take a screen shot with status bar contents in android application?
Use MediaProjectionManager#createScreenCaptureIntent given in the MediaProjection API of Android. What you are doing now is taking the chatHead's root view's information rather than information present on the complete screen of your device including the status bar at the top.
Related
I have tried about 10 answers to a similar question, but none of them were successful. Something might be out of date. Please give an answer for today.
Simple task:
I want my app to be able to send a file with an image to e-mail or whatsapp.
I put the file with the image in the application resources in the folder Drawable.
Its size is 5 MB.
How to do this in Java?
I'm asking for code, not links to similar answers. I've tried most of them already.
Some decisions came to null.bin.
Some don't send anything at all and throw an error.
Perhaps I am not specifying any permissions.
Something else is possible.
I'm new.
I would like an answer with comments (what I am writing and why)
// step 1. I put the picture in the folder Drawable
// clicking on the button sends the picture through the messenger to other users
public void onClick_sendMyImage(View v) {
// Step 2.Convert PICTURE to Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
saveImage(bitmap); // 3. save the PICTURE in the internal folder
send(); // 4.sending PICTURE (function code below)
}
// step 3. Saving the image in the internal folder:
private static void saveImage(Bitmap finalBitmap){
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File myDir = new File(root + "/saved_images");
//Log.i("Directory", "==" + myDir);
myDir.mkdirs();
String fname = "image_test" + ".jpg";
File file = new File(myDir, fname);
if(file.exists()) file.delete();
try{
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// Step 4. Sending the saved PICTURE
public void send(){
try{
File myFile = new File("/storage/emulated/0/saved_images/image_test.jpg");
// for something I create this type (so it is said in one of the answers)
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(type);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myFile));
Intent intent1 = Intent.createChooser(intent, "what do you want to send ?");
startActivity(intent1);
}catch (Exception e){
Toast.makeText(this, "repeat sending", Toast.LENGTH_SHORT).show();
}
}
Sending an image from an app turned out to be not a good idea.
The image cannot be edited if necessary.
Over time, you may want to add more images, etc..
I did it differently, and it turned out better:
I put the image on GoogleDisk.(now I can change it at any time without having
to ask users to reinstall the application every time the picture changes).
I placed a link to the picture in the FireBase Database. (I also placed a banner with a picture here.).
I connected the application to the Firebase Database.
In the activity I made a RecyclingView, where I get a banner(s) and a link(s) to the original image(s). The user can download this link or send it through whatsapp anywhere and download the original picture in good quality (A1, 2.5Mb).
thanks, guys..
I thank the guys for the tips, especially mr.Blackapps. In this case, you really can't do without a FileProvider.
I am working on a project for my course and have been asked to show an associated image for products in a system, the user can add products, and he enters the name of the image file whilst doing so. the program then finds the image file and displays it with the product information.
I would like to add some additional code so that if there is no image file matching the string input that a standard image is shown. The code I have so far either shows the image file if it is found or does not show anything. can someone show me how to modify it so that it can show a standard image if no associated image file is found. the standard image is simply "no-image-found.jpg". Here is the code:
public void showImage(JLabel imageArea, String image){
BufferedImage img = null;
try {
img = (BufferedImage)ImageIO.read(new File(image));
Image actualimage = img.getScaledInstance(imageArea.getWidth(), imageArea.getHeight(), 0);
imageArea.setIcon(new ImageIcon(actualimage));
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
any help is very much appreciated, and my sincere apologies if this is a noob question, I am quite new to java.
In the catch, you could load the standard image and show it
Basically check for image existence assuming path is valid otherwise get the standard image. You can have something similar:
public void showImage(JLabel imageArea, String image)
{
BufferedImage img = null;
try
{
file = new File(image);
if (file.exists())
{
img = ImageIO.read(file);
}
else
{
file = new File(standardImagePath);
img = ImageIO.read(file);
}
Image actualimage = img.getScaledInstance(imageArea.getWidth(), imageArea.getHeight(), 0);
imageArea.setIcon(new ImageIcon(actualimage));
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
im currently trying to implement a webcam scanner. My current problem is that i can't capture a high quality image when using Java.
The camera i use has 8MP and produces great images when i use the Windows "Camera"-App. Sadly i can't replicate this quality with JavaCV.
This is the (important) code i'm using to capture an Image:
FrameGrabber grabber;
private Java2DFrameConverter frameConverter;
public JavaCVCamera(String imagePath, double rotation) {
this.frameConverter = new Java2DFrameConverter();
this.imagePath = imagePath;
this.rotation = rotation;
this.grabber = new OpenCVFrameGrabber(0);
this.grabber.setImageWidth(3264);
this.grabber.setImageHeight(2448);
try {
this.grabber.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void saveImageToFile(boolean applyUpscaling, boolean applyRotation) {
File folder = new File(imagePath);
if (!folder.exists()) {
folder.mkdirs();
}
try {
BufferedImage img = frameConverter.convert(grabber.grab());
if (applyUpscaling) {
img = Thumbnails.of(img).forceSize(3264, 2448).asBufferedImage();
}
if (applyRotation) {
img = rotateImageByDegrees(img, rotation);
}
File path = new File(imagePath + "/img_"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss")) + "." + "png");
ImageIO.write(img, "png", path);
LOGGER.info("Captured Image: {}", path.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
(Note: i dont use upscaling to make the images larger)
For your understanding, here's an example:
Captured with Windows Camera App
https://imgur.com/a/oOuMJdl
Captured with Java+JavaCV
https://imgur.com/a/DghFT8z
Is there some way to capture images with the same resolution and quality as the Windows Camera App?
Kind Regards,
QUE
A BufferedImage is very memory consuming in Java as it stores the image in a Bitmap like image format. Other image formats that use compression consume less memory. But how to use them with JavaCV?
First of all, check with a profiler app which code is responsible for the extensive memory consumption of your implementation. Hopefully the Frame stores the image data still in compressed form; and when executing the frameConverter.convert it consumes the high amount of memory. In this case, search for a solution that stores the image as a JPEG (or any other compressed data) in a byte[] - for displaying the image, you can draw the image directly e.g. to a Graphics2D element w/o converting it to a BufferedImage first. Use ToolkitImage therefore, which is able to draw a JPEG image from an InputStream image source. This can be a ByteArrayInputStream, if you were able to store your high-res image in a JPEG directly from the JavaCV.
BACKGROUND
Hey so I have a camera that I have implemented myself in code. This means I access and control the camera hardware and use it to save pictures. I can save the picture using the Camera.takePicture() function when the camera is running: running means Camera.startPreview();
PROBLEM
My problem is that I want to be able to save the image also when the camera image is frozen: frozen is when Camera.stopPreview(); is called.When frozen I can see the image in my layout but how do I access it? Where is the image saved so that I might be able to modify it later?
Thanks in advance!
------------------Update 1
jpeg bla;
public class jpeg implements PictureCallback{
public void onPictureTaken(byte[] data, Camera camera) {
g_data = data;
}
}
This is part of my code. Here I am trying to write the data that would originally be saved to a global variable. However the value of g_data remains null and I am unable to set a breakpoint inside the onPictureTaken() call back function.
------------------Update 2
FileOutputStream outStream = null;
try {
// generate the folder
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MirrorMirror");
if( !imagesFolder.exists() ) {
imagesFolder.mkdirs();
}
// generate new image name
SimpleDateFormat formatter = new SimpleDateFormat("HH_mm_ss");
Date now = new Date();
String fileName = "image_" + formatter.format(now) + ".jpg";
// create outstream and write data
File image = new File(imagesFolder, fileName);
outStream = new FileOutputStream(image);
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) { // <10>
//Toast.makeText(ctx, "Exception #2", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {}
I used this code previously to save the file from the camera onPictureTaken() function. The key here is the byte[] data which I need to save and save later. However like I said I just get a null when I check it in the debugger.
Camera.takePicture never looks at the view you specified as previewDisplay. Actually, it isn't an ImageView, but a SurfaceView, and there are no API to read pixels from it.
You can call takePicture() preemptively just before you stopPreview(). Later, if you find out that you don't need the picture, just discard it.
Ok so the exact way to do this is to take the picture just before stopPreview() and save it to a temporary file. Actually with this implementation you never call stopPreview()(otherwise it will crash) since the takePicture() function stops the preview automatically.
try {
File temp = File.createTempFile("temp", ".jpg");
} catch (IOException e) {
e.printStackTrace();
}
Now that we have the temporary file saved we will access it later and move it to our new desired file location.
How to copy file.
temp.deleteOnExit();
be sure to call deleteonExit() so that Android deletes the file after the app is closed(if so desired).
I want to get a web page (html, php?, javascript?) stored on my Android phone's SD card, to get an image from a specified url and save it back to the SD card.
Is this possible and if so in what language can this be achieved? Any help would be much appreciated even if you can just say yes and point me in the right direction. I'm happy to tinker around with code but don't even know where to start or if this is simply not possible.
Thanks
If you have url to get image then this code will help you
String myfeed="your url";
URL url;
try {
url = new URL(myfeed);
InputStream content = (InputStream)url.getContent();
d = Drawable.createFromStream(content , "src");
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
String path = Images.Media.insertImage(getContentResolver(),bitmap,mImageName, null);
screenshotUri = Uri.parse(path);
System.out.println("your path :"+screenshotUri);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}