So I'm trying to write my encoded buffered image to an output stream but I can't get any data to come through on the stream... Could anyone tell me what I'm doing wrong and why I can't see any output?
I would expect that when I call the write.encodeVideo method that it encode's video into my ByteArrayOutputStream... is that assumption wrong?
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// video parameters
final int videoStreamIndex = 0;
final int videoStreamId = 0;
final long frameRate = DEFAULT_TIME_UNIT.convert(15, MILLISECONDS);
final int width = 512;
final int height = 254;
long nextFrameTime = 0;
// create a media writer and specify the output file
final IMediaWriter writer = ToolFactory.makeWriter("aaa.ogg");
IContainer ic = writer.getContainer();
ic.open(outputStream, writer.getContainer().getContainerFormat(), true, false);
ICodec codec = ICodec.guessEncodingCodec(null, null,"aaa.ogg", null, ICodec.Type.CODEC_TYPE_VIDEO);
// add the video stream
writer.addVideoStream(videoStreamIndex, videoStreamId, codec, width, height);
BufferedImage img = null;
try
{
img = ImageIO.read(new File("/data/aaa.png"));
}
catch (final IOException e)
{
e.printStackTrace();
}
for(int yy =0; yy < 2048-height; yy=yy+8)
{
nextFrameTime++;
BufferedImage frame = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
frame = img.getSubimage(0, yy, width, height);
BufferedImage frame2 = convertToType(frame, BufferedImage.TYPE_3BYTE_BGR);
//encode the video
writer.encodeVideo(videoStreamIndex, frame2, nextFrameTime, DEFAULT_TIME_UNIT);
nextFrameTime += frameRate;
}
writer.close();
outputStream.flush();
outputStream.close();
I believe the way you are creating the IMediaWriter is incorrect. You don't want to open the writer's container yourself either. When you are using an OutputStream instead of a file you can do it by using the com.xuggle.xuggler.io.XugglerIO mapper like this:
// create a media writer and specify the output stream
final IMediaWriter writer = ToolFactory.makeWriter(XugglerIO.map(outputStream));
// manually set the container format (because it can't detect it by filename anymore)
IContainerFormat containerFormat = IContainerFormat.make();
containerFormat.setOutputFormat("ogg", null, "application/ogg");
mWriter.getContainer().setFormat(containerFormat);
// add the video stream
writer.addVideoStream(videoStreamIndex, videoStreamId, ICodec.ID.CODEC_ID_THEORA, width, height);
Keep in mind that if you are trying to create a format that has to be able to "seek" within the output bytes as part of it's creation process (e.g. .mov), then it won't work with a simple OutputStream as I have shown. You would have to write to a file instead of an OutputStream.
Related
I'm using this code for image compression before uploading the images :
public File saveBitmapToFile(File file) {
try {
// BitmapFactory options to downsize the image
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inSampleSize = 6;
// factor of downsizing the image
FileInputStream inputStream = new FileInputStream(file);
//Bitmap selectedBitmap = null;
BitmapFactory.decodeStream(inputStream, null, o);
inputStream.close();
// The new size we want to scale to
final int REQUIRED_SIZE = 75;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
inputStream = new FileInputStream(file);
Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
inputStream.close();
FileOutputStream outputStream = new FileOutputStream(file);
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return file;
} catch (Exception e) {
return null;
}
}
The problem is the original image will be affected and get resized.
How to compress images without overwriting and losing the original one?
Update 1 :
I changed the last part of the code to this but still doesn't work.
now the image wouldn't get resized
File new_file =new File("/storage/emulated/0/DCIM/Screenshots/tmp.png");
try
{
new_file.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
Log.d("Create File", "File exists?"+new_file.exists());
FileOutputStream outputStream = new FileOutputStream(new_file);
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return file;
Update 2 :
I've changed the code to this so the problem is partly solved. Now I can have an original quality of each image in a file named tmp"+new Date()+".png but the original file still will be overwritten.
File new_file =new File(String.valueOf("/storage/emulated/0/DCIM/Screenshots/tmp"+new Date()+".png"));
try
{
new_file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(new_file, true);
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
Log.d("Create File", "File exists?"+new_file.exists());
FileOutputStream outputStream = new FileOutputStream(file);
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
Create a new File and make the FileOutputStream write to it, rather than writing to the original.
New Answer
Your case is quite unique, what if you try this:
Rather than directly using the Bitmap from InputStream, try use it's copy().
That way, the one that you compress will be the copy of the Bitmap. And you can compress that in your new FileOutputStream without modifying the original file.
And remove the second compression. Just dont do anything with your original file.
How would I go about writing a javafx.scene.image.Image image to a file. I know you can use ImageIO on BufferedImages but is there any way to do it with a javafx Image?
Just convert it to a BufferedImage first, using javafx.embed.swing.SwingFXUtils:
Image image = ... ; // javafx.scene.image.Image
String format = ... ;
File file = ... ;
ImageIO.write(SwingFXUtils.fromFXImage(image, null), format, file);
Almost 3 years later and I now have the knowledge to do and answer this. Yes the original answer was also valid but it involved first converting the image to a BufferedImage and I ideally wanted to avoid swing entirely. While this does output the raw RGBA version of the image that's good enough for what I needed to do. I actually could just use raw BGRA since I was writing the software to open the result but since gimp can't open that I figure I'd convert it to RGBA.
Image img = new Image("file:test.png");
int width = (int) img.getWidth();
int height = (int) img.getHeight();
PixelReader reader = img.getPixelReader();
byte[] buffer = new byte[width * height * 4];
WritablePixelFormat<ByteBuffer> format = PixelFormat.getByteBgraInstance();
reader.getPixels(0, 0, width, height, format, buffer, 0, width * 4);
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("test.data"));
for(int count = 0; count < buffer.length; count += 4) {
out.write(buffer[count + 2]);
out.write(buffer[count + 1]);
out.write(buffer[count]);
out.write(buffer[count + 3]);
}
out.flush();
out.close();
} catch(IOException e) {
e.printStackTrace();
}
JavaFX has no built-in method to do this.
To solve this problem, I implemented a very small (< 20KiB) library for writing PNG files: https://github.com/Glavo/SimplePNG
Usage:
Image img = new Image("path-to-image.jpg");
try (PNGWriter writer = new PNGWriter(Files.newOutputStream(Path.of("output.png")))) {
writer.write(PNGJavaFXUtils.asArgbImage(img));
}
// Or you can use the shortcut:
// PNGJavaFXUtils.writeImage(img, Path.of("output.png"));
It has no dependencies and can work on the JRE that only have java.base.
I avoid the dependence on Java AWT (java.desktop) through it.
I am trying to merge two TIFF images which are in form of FileInputStream into a single Tiff image. Although the image is getting merged the output file is coming up as Black. While comparing the original image and the converted image I could see that the bit depth of the converted image changes to 1. Could anybody provide a solution to this?
The code that I am using is:
public class MergerTiffUsingBuffer {
public static void main(String[] args) {
File imageFile1 = new File("D:/Software/pdfbox-1.3.1.jar/tiff/FLAG_T24.TIF");
File imageFile2 = new File("D:/Software/pdfbox-1.3.1.jar/tiff/CCITT_3.TIF");
try {
FileInputStream fis1 = new FileInputStream(imageFile1);
FileInputStream fis2 = new FileInputStream(imageFile2);
List<BufferedImage> bufferedImages=new ArrayList<>();
List<FileInputStream> inputStreams=new ArrayList<>();
inputStreams.add(fis1);
inputStreams.add(fis2);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("tiff");
ImageReader reader = (ImageReader) readers.next();
for(FileInputStream inputStream:inputStreams){
ImageInputStream iis = ImageIO.createImageInputStream(inputStream);
reader.setInput(iis);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0, param);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
OutputStream out = new FileOutputStream("D:/Software/pdfbox-1.3.1.jar/tiff/MergedTiff.TIF");
BufferedImage binarized = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
ImageIO.write(binarized, "tiff", out);
bufferedImages.add(bufferedImage);
}
System.out.println(bufferedImages.size());
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
You seem to be a little confused about how to copy image data. Simply creating a new, blank image, by passing the dimensions of another image, will not copy it... So a fully black image is what I would expect after running your code.
Replace your for loop with something like this:
for (FileInputStream inputStream : inputStreams) {
ImageInputStream iis = ImageIO.createImageInputStream(inputStream);
reader.setInput(iis);
BufferedImage image = reader.read(0, null); // a) BufferedImage is returned! b) null param is fine!
BufferedImage binarized = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
// The following 7 lines is the important part you were missing:
Graphics2D g = binarized.createGraphics();
try {
g.drawImage(image, 0, 0, null);
}
finally {
g.dispose();
}
OutputStream out = new FileOutputStream("D:/Software/pdfbox-1.3.1.jar/tiff/MergedTiff.TIF");
ImageIO.write(binarized, "tiff", out); // You probably want to check return value (true/false)!
bufferedImages.add(image);
}
Capture a image from the clipboard whenever Print screen is pressed and save it in a file (.doc) using java
Main aim is to copy the data from the clipboard and automatically save it into local disk without going to the desired program (i.e MS Word)- click new - pressing (Ctrl+V) to paste and save it with a name.
The code should perform all of the above three steps automatically.
My Source Code
public class CaptureScreenShot {
private static String DIR ="C:\\QUIS\\";
private static JTextField txtDocNumber;
public static void main(String[] args) throws Exception{
txtDocNumber = new JTextField();
Robot robot = new Robot();
Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
int width = (int) d.getWidth();
int height = (int) d.getHeight();
robot.delay(5000);
Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
height));
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
String fileNameToSaveTo = "C:/QUIS/screenCapture_" + createTimeStampStr() + ".PNG";
String newFile = "C:/QUIS/x" + ".org";
File newFilee = new File(newFile);
writeImage(bi, fileNameToSaveTo, "PNG");
System.out.println("Screen Captured Successfully and Saved to:\n"+fileNameToSaveTo);
Desktop desktop = Desktop.getDesktop();
writeImage(bi, newFile, "org");
desktop.open(newFilee);
}
public static int writeImage(BufferedImage img, String fileLocation,
String extension) {
try {
BufferedImage bi = img;
File outputfile = new File(fileLocation);
ImageIO.write(bi, extension, outputfile);
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
public static String createTimeStampStr() throws Exception {
Calendar mycalendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
String timeStamp = formatter.format(mycalendar.getTime());
return timeStamp;
}
}
If you are not particular about a java code, you could use some screen capture tool instead. Snagit is a good tool. You could find it on http://www.techsmith.com/snagit.html
Try this sample code, according to your question it will copy contents from clipboard and will generate image file
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
//Get data from clipboard and assign it to an image.
//clipboard.getData() returns an object, so we need to cast it to a BufferdImage.
BufferedImage image = (BufferedImage)clipboard.getData(DataFlavor.imageFlavor);
//file that we'll save to disk.
File file = new File("image.jpg");
//class to write image to disk. You specify the image to be saved, its type,
// and then the file in which to write the image data.
ImageIO.write(image, "jpg", file);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert pdf file to jpg asp.net
public class Pdf2Image {
private Image image;
int length;
public int convertPdf2Image(String pdfname) {
File file = new File(pdfname);
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// draw the first page to an image
int num = pdffile.getNumPages();
length=num;
for (int i = 0; i <= num; i++) {
PDFPage page = pdffile.getPage(i);
//get the width and height for the doc at the default zoom
int width = (int) page.getBBox().getWidth();
int height = (int) page.getBBox().getHeight();
Rectangle rect = new Rectangle(0, 0, width, height);
int rotation = page.getRotation();
Rectangle rect1 = rect;
if (rotation == 90 || rotation == 270) {
rect1 = new Rectangle(0, 0, rect.height, rect.width);
}
//generate the image
BufferedImage img = (BufferedImage) page.getImage(
rect.width, rect.height, //width & height
rect1, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
ImageIO.write(img, "png", new File("src\\downloadedFiles\\aa" + i + ".png"));
}
} catch (FileNotFoundException e1) {
System.err.println(e1.getLocalizedMessage());
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
}
return length;
}
public static void main(String[] args) {
Pdf2Image p = new Pdf2Image();
p.convertPdf2Image("src\\downloadedFiles\\todaypdf.pdf");
}
}
I am using this code to convert PDF file to image. It is working fine for most of the PDF's but showing exception for a PDF file. Exception is:
Expected 'xref' at start of table.
Could any one tell me why it is giving such an exception?
There are many malformed PDF files out in the wild and this is most likely one of them.
It is not possible to give a definite answer until seeing the problem PDF file. What I am guessing is that the 'startxref' specifies an absolute position into the PDF where the xref table should be located. The java library is jumping to this position on the file expecting to find the word 'xref' but cannot find it.
http://blog.amyuni.com/?p=1627
One way to fix this would be to load the file into the full version of Acrobat and then save the file. Acrobat will fix the xref offset as mentioned in the link.
There are quite large companies out there generating malformed PDF's that should know better. Adobe lets these files exist because it makes it hard for their PDF competitors to keep up and compete.