I'm trying to take a screenshot of my Android activity (bitmap) and then making a PDF file out of it. I used the itextpdf library. Here's what I have:
public void onSaveDataClicked(View reportsLayout){
//take screen shot
Bitmap screen; View v1 = reportsLayout.getRootView();
v1.setDrawingCacheEnabled(true);
screen = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
}
catch (Exception e){
e.printStackTrace();
}
}
private static void addImage(Document document,byte[] byteArray) {
Image image = null;
try {
image = Image.getInstance(byteArray);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try {
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I try to run I get the errors: "Image cannot be converted to Element" and "cannot find symbol method getInstance(byte[])"
A lot of the code I found online through a tutorial on how to achieve this. I'm kind of unfamiliar with this sort of thing. Any help and ideas would be appreciated.
Use import com.itextpdf.text.Image;and remove the default Android one.
Related
I have a project which creating ms word document. In this word document, I want to create header/footer. To do this I wrote this codes
public static void header_footer(String SalesOrder,String type) throws InvalidFormatException, IOException
{
XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
if (headerFooterPolicy == null)
headerFooterPolicy = document.createHeaderFooterPolicy();
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFTable asd= header.createTable(1, 2);
asd.setTableAlignment(TableRowAlign.LEFT);
XWPFTableRow row;
XWPFTableCell cell,cell2;
CTHeight ht;
CTTrPr trPr;
CTTblWidth cellWidth;
row= asd.getRow(0);
trPr = row.getCtRow().addNewTrPr();
ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(-1070));
cell=row.getCell(0);
XWPFParagraph parag=document.createParagraph();
parag = cell.getParagraphArray(0);
XWPFRun r1=parag.createRun();
InputStream pic = access_connection.class.getResourceAsStream("page1_header.png");
r1.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, imgFile2, Units.toEMU(160), Units.toEMU(55));
cellWidth = cell.getCTTc().addNewTcPr().addNewTcW();
cellWidth.setW(BigInteger.valueOf(3500));
cell2=row.getCell(1);
cellWidth = cell2.getCTTc().addNewTcPr().addNewTcW();
cellWidth.setW(BigInteger.valueOf((short)20000));
parag=cell2.getParagraphArray(0);
parag.setVerticalAlignment(TextAlignment.CENTER);
parag.setAlignment(ParagraphAlignment.CENTER);
cell2.setVerticalAlignment(XWPFVertAlign.CENTER);
XWPFRun r2=parag.createRun();
r2.setFontSize(14);
r2.setFontFamily("Calibri");
r2.setText("TURKISH ENGINE CENTER SHOP VISIT REPORT");
r2.addBreak();
r2.setText("ESN "+SalesOrder.substring(1, 7)+" Istanbul,TURKEY");
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph parag2= footer.createParagraph();
XWPFRun run3=parag2.createRun();
XWPFRun run4=parag2.createRun();
run3.setFontFamily("Arial");
run4.setFontFamily("Arial");
run3.setBold(true);
run4.setItalic(true);
run3.setFontSize(8);
run4.setFontSize(9);
run3.setText("U.S. Export Classification: EAR ECCN 9E991");
run3.addBreak();
run4.setText("Printed by TEC Local EVM System");
XWPFParagraph parag3= footer.createParagraph();
XWPFRun run5=parag3.createRun();
parag3.setAlignment(ParagraphAlignment.RIGHT);
run5.setFontSize(7);
run5.setFontFamily("Arial");
run5.setText("Page ");
run5.getCTR().addNewPgNum();
// footer.setCommited(true);
try {
FileOutputStream input = new FileOutputStream(word_saveas_file+SalesOrder+"_"+type+"_Report.docx");
document.write(input);
input.close();
// input.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm running my project from .jsp web page on tomcat server. When I run my project first time, everything running correctly. But when I run my project second time without stopping or restarting tomcat server, header/footer is repeating. I don't understand why only header/footer repeating. I'm creating ms word documents which are different names but still have this problem.
public static void Start(String SalesOrder, String path,String type) throws SQLException, ClassNotFoundException {
baslik[0]="ATA";
baslik[1]="LID";
baslik[2]="PN";
baslik[3]="QPE";
baslik[4]="SV";
baslik[5]="IS";
baslik[6]="RS";
baslik[7]="REP";
baslik[8]="Defect";
baslik2[0]="PN";
baslik2[1]="NEW";
baslik2[2]="REP";
baslik2[3]="SV";
baslik2[4]="Information Only";
try {
start_page_1(SalesOrder,path,type);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
start_page_2(SalesOrder,type);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
header_footer(SalesOrder,type);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
eng_general(SalesOrder);
oil_sys(SalesOrder);
GroupName(1);
partoff(SalesOrder,type);
//baglanti= DriverManager.getConnection("jdbc:ucanaccess://"+db_file,db_username,db_password);
//sorgu=baglanti.createStatement();
//deneme=sorgu.executeQuery("SELECT * FROM TableRun");
System.out.println("Process Completed");
}
public static void Start_PDM(String SalesOrder, String path,String type) throws SQLException, ClassNotFoundException, IOException {
baslik[0]="ATA";
baslik[1]="LID";
baslik[2]="PN";
baslik[3]="QPE";
baslik[4]="SV";
baslik[5]="IS";
baslik[6]="RS";
baslik[7]="REP";
baslik[8]="Defect";
baslik2[0]="PN";
baslik2[1]="NEW";
baslik2[2]="REP";
baslik2[3]="SV";
baslik2[4]="Information Only";
try {
start_page_1(SalesOrder,path,type);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
start_page_2(SalesOrder,type);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
header_footer(SalesOrder,type);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GroupName(1);
partoff(SalesOrder,type);
/*try {
Runtime.getRuntime().exec("taskkill /F /IM java.exe");
Runtime.getRuntime().exec("net start java");
System.exit(0);
} catch (Throwable e) {
} */
baglanti.close();
//baglanti= DriverManager.getConnection("jdbc:ucanaccess://"+db_file,db_username,db_password);
//sorgu=baglanti.createStatement();
//deneme=sorgu.executeQuery("SELECT * FROM TableRun");
System.out.println("Process Completed");
}
I'm using first method for create word document with eng_general and oil_sys methods. In the second method I'm creating word document without eng_general and oil_sys.
I'm trying to download an image with exif data from URL to my pc, the problem is that exif data is gone after the image has been downloaded.
here's the code i use
public static void downloadImage(String str) {
try {
URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/6/68/Goldmantelziesel.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("F:\\Photos\\ww123\\"+str+".jpg");
ImageIO.write(img, "jpg", file);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
I need to save a captured image from camera on a tmp file and retrieve it on the next activity, so i can handle better the OOM error.
So i did something like this to write the file:
public void savePicAtFile(Bitmap pic){
FileOutputStream out = null;
try {
out = new FileOutputStream("tmpCameraPic");
pic.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
now i don't know how to retrieve the compressed image, should i just create a bitmap and getall the stream?
Any help?
I used itext to create an rtf file. I want add a background color to the text with the following code:
public static void main(String[]args) throws IOException{
Document document = new Document();
try {
RtfWriter2 rtf = RtfWriter2.getInstance(document, new FileOutputStream("text.rtf"));
document.open();
Chunk chunk = new Chunk("This is a text");
chunk.setBackground(new Color(0xFF,0x00,0x00),0f,0f,0f,0f);
Phrase fa = new Phrase();
fa.add(chunk);
Paragraph p = new Paragraph();
p.add(fa);
document.add(p);
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I open it in Mac, I cannot see the background color shown. But when I try to use Word to open it, the background color is shown. Thus,
Is it the right way to highlight a text block in rtf?
When I use JEditorPane to open it, the background color still cannot show.
I have the code to capture image from screen in java, I have the final captured image as BufferedImage object and Can cast it to ImageIcon
The problem is when sending that file to android can't read it as bitmap drawable. Any one have answer to this ?
Code to send (Java)
BufferedImage image = robot.createScreenCapture(rectangle);
ImageIcon imageIcon = new ImageIcon(image);
//Send captured screen to the server
try {
System.out.println("before sending image");
oos.writeObject(imageIcon);
oos.reset(); //Clear ObjectOutputStream cache
System.out.println("New screenshot sent");
} catch (IOException ex) {
ex.printStackTrace();
}
Android Receiver Part
Thread t= new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
client= sc.accept();
is = client.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapDrawable imageIcon = null;
try {
ois = new ObjectInputStream(is);
imageIcon = (BitmapDrawable) ois.readObject();
//Drawable d = Drawable.createFromStream(is, null);
IV.setImageDrawable(imageIcon);
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("New image recieved");
}
}
I get the exception of it can't cast the imageIcon or the BufferedImage to Bitmap drawable.
You should not use objects, because oracle jvm is not the same as android's dalvik.
Try to convert captured image to raw bytes and send those bytes to android. Then in android covert this raw data to drawable.