OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Change Picture for OpenOffcie (BASIC)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
ArielEnter
General User
General User


Joined: 11 Jun 2009
Posts: 25

PostPosted: Fri Nov 06, 2009 4:12 pm    Post subject: Change Picture for OpenOffcie (BASIC) Reply with quote

Change Picture is a macro I made for OpenOffice that lets you change the picture of an existing image in a document.
http://www.youtube.com/watch?v=DjmkDVyMmn4
http://extensions.services.openoffice.org/project/ChangePicture

Este es una macro que hice para OpenOffice que te permite cambiar la fotografiá de un imagen existente en un documento.

I want to thank james_h, if he wouldn't had encourage me to keep working on it I wouldn't have made the extension. Thanks man, I really appreciate your support and advising I'm really in debt with you.

I want to thank every body in this forum, since I was in a rush I didn't take note of all the post I had to go trough. I'll say there were more than 50 (I'm not a quick learner) and most of the time it was copy and paste and trying to understand the code. Thank you all.

Also I need to thank Paolo Mantovani for his BasicAddonBuilder - The Extension's packager is a great tool:
http://extensions.services.openoffice.org/project/BasicAddonBuilder

Hope you find this macro use full. Talking about that, it hasn't pass a day and I already have 284 downloads on my extension. This is the first time I get so mush attention in the Internet. Thank you all.


Last edited by ArielEnter on Mon Nov 23, 2009 1:58 pm; edited 19 times in total
Back to top
View user's profile Send private message
ArielEnter
General User
General User


Joined: 11 Jun 2009
Posts: 25

PostPosted: Fri Nov 06, 2009 5:46 pm    Post subject: commented Reply with quote

Deleted
This is the code

Code:
REM  *****  BASIC  *****
sub Entire
               ' * Most of the code is explained in the variables declarations below
   Dim oDoc As Object       ' Working document
   Dim oElements As Object   ' All elements that are currently selected by the user
   Dim oFirst As Object   ' Is the first element of all currently selected elements that may or may not be an image
   Dim oAllFiles As Object   ' All files' url that were selected in the file picker dialog
   Dim oFirstFile         ' The first file's url of all the files selected in the file picker dialog
   Dim oDialog As Object   ' File picker dialog
   Dim oBitmaps as object ' holds the pictures embedded to the document
   oDoc = thisComponent   '*
   Dim s1$         ' Graphic service name
   Dim s2$         ' Graphic service name
   On Error Goto ErrorHandler
   oElements = oDoc.getCurrentSelection() '*
    oFirst = oElements(0) '*
      s1 = "com.sun.star.drawing.GraphicObjectShape"
   s2 = "com.sun.star.text.TextGraphicObject"
   If not(oFirst.supportsService(s1) OR oFirst.supportsService(s2)) Then ' Checks if oFirst is not an image
      msgbox( NO_IMAGE_IS_SELECTED & " " & UNABLE_TO_REPLACE_IMAGE  ,64, CHANGE_PICTURE) 'If is not an image the program will stop
      exit sub
   else
      oImg = oFirst 'In the other hand, if it is a image, we will intent to change its picture
   end if
   oDialog = createUnoService("com.sun.star.ui.dialogs.FilePicker") ' *
   sFilePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_LINK_PREVIEW) 'Specify what kind of dialog it will be. In our case is a file picker.
   oDialog.Initialize(sFilePickerArgs()) ' We initialize the dialog as a FILEOPEN_LINK_PREVIEW
   oDialog.AppendFilter( "All Image Formats (*.jpeg;*.jpg;*.gif;*.png;*.tiff;*.tif;*.bmp;...", "*.jpeg;*.jpg;*.gif;*.png;*.tiff;*.tif;*.bmp;*.dxf;*.emf;*.eps;*.met;*.pbm;*.pcd;*.pct,*.psx;*.pgm;*.ppm;*.psd;*.ras;*.sgf;*.ras;*.sgf;*.sgv;*.svm;*.tga;*wmf;*.xbm;*.xpm") ' We tell the dialog to display only image formats to chose from
   if oDialog.execute = 0 then ' Will display the dialog and if the user decided to close the dialog instead of chosen a file it will return 0 in which case it'll end up the program
      exit sub
   end if
    oAllFiles() = oDialog.getFiles() '*
    oFirstFile = oAllFiles(0) '*
   if oDialog.getValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 0) = True then 'If the user chooses to link the file instead of embed it, the check box will be true
         oImg.GraphicURL = oFirstFile ' gives to the image the hard drive location of the file and ends up the program
         'oDoc.refresh() it didn't work in Cal, I tried in a couple of computers and it wasn't needed anymore
         exit sub
      
   end if
      oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" ) '*
      oBitmaps.insertByName("Image", oFirstFile) ' we store the image in the bitmap holder
       oImg.GraphicURL = oBitmaps.getByName("Image") ' we give the image the location of the picture which is stored within the document
      oBitmaps.removeByName("Image") 'once that we have given the image a picture we no longer need it inside of the oBitmap
      'oDoc.refresh() 'Seems not needed anymore
   exit sub
ErrorHandler:
         msgbox( NO_IMAGE_IS_SELECTED & " " & UNABLE_TO_REPLACE_IMAGE  ,48, CHANGE_PICTURE)
End Sub


This code is over GPL 3 or later.


Last edited by ArielEnter on Mon Aug 30, 2010 11:48 am; edited 2 times in total
Back to top
View user's profile Send private message
james_h
Super User
Super User


Joined: 05 Nov 2005
Posts: 873

PostPosted: Fri Nov 06, 2009 9:37 pm    Post subject: Test if selection supports Service Reply with quote

Quote:
One of the things I wish I could have changed is getting a better way to find out if the selected object is an image or not.

Your code might be able to use the thisComponent.getCurrentSelection.supportsService("NameOfService") test to determine what has been selected. I haven't tested this snippet extensively, but you should get an idea from it.
Code:
Sub testGraph
oDocument = thisComponent
oSelection = oDocument.getCurrentSelection()
if oSelection.supportsService("com.sun.star.text.TextRanges") then
  MsgBox ("Selection is text")
else
  MsgBox ("Selection is image")
end if
end sub
As far as I know, TextRanges are only applicable to writer documents. Also, any objects that are not text (for example, Drawing Objects, Embedded OLE Objects) are rated as images according to this snippet. What I have not yet determined is the name of a service that is unique to inserted jpeg, png, tif and gif images.

This looks like an interesting utility. When you've fine tuned it, maybe you should package it as an extension.
Back to top
View user's profile Send private message
ArielEnter
General User
General User


Joined: 11 Jun 2009
Posts: 25

PostPosted: Fri Nov 20, 2009 3:55 pm    Post subject: Reply with quote

One thing I was having troubles with was the concept of a Bitmap table. I haven't read about the matter, and I never got a lesson in school about that, so I may be wrong. For me, a bitmap table is variant that holds the picture itself in the document. So in order to embed an image to the document, instead of referring to a location in the file system of the OS (the hard drive location) you tell the image object to take the picture from the bit map table located inside the document it self.

While making the program, I was afraid of leaving in the bit map table images that were no longer necessary every time Picture changed, making the document unnecessarily bigger and bigger.

According to my personal conception of a bitmap table, if you delete a picture of the bitmap table, all images using that same picture will lost the ability to display that picture. But, by testing it my self, and as my Change Picture Extension shows, this is not true.

Code:
       oImg.GraphicURL = oBitmaps.getByName("Image") ' we give the image the location of the picture which is stored within the document
      oBitmaps.removeByName("Image") 'once that we have given the image a picture we no longer need it inside of the oBitmap


The only thing I can think of is that the Image object is capable of holding the picture itself. Now this is good, because it means a more efficient control over the size of the document.

If the image object is deleted, the picture is deleted too and no longer exist inside of the document. This will mean that the size of the document will decrease as is logical. And since all entries in the bitmap table are deleted right after they are used too, the bitmap table doesn't take any space in the document either.

Please, could someone tell me if I'm right in my concepts? I'll really appreciate it.

I have tested and the program works very well, and pictures are not lost when you move them to another computer if they were embedded and even if the bitmap talble is empty.


Last edited by ArielEnter on Mon Aug 30, 2010 11:43 am; edited 1 time in total
Back to top
View user's profile Send private message
james_h
Super User
Super User


Joined: 05 Nov 2005
Posts: 873

PostPosted: Mon Nov 23, 2009 6:15 pm    Post subject: Image - How to determine whether it is linked or embedded Reply with quote

An image in an OpenOffice.org document can be either embedded in the document itself or a link to an external image. The OpenOffice.org help says:
Quote:
Inserting Bitmaps
A bitmap image can be inserted in OpenOffice.org Writer, OpenOffice.org Calc, OpenOffice.org Draw and OpenOffice.org Impress documents.
1.Choose Insert - Picture - From File.
2.Select the file. In the File type box you can restrict the selection to certain file types.
3.Click the Link box if you want a link to the original file.
If the Link box is marked, whenever the document is updated and loaded the bitmap image is reloaded. The editing steps that you have carried out in the local copy of the image in the document are re-applied and the image is displayed.
If the Link box is not marked, you are always working with the copy created when the graphic was first inserted.
To embed graphics that were first inserted as links, go to Edit - Links and click the Break Link button.
4.Click Open to insert the image.


ArielEnter wrote:
Quote:
While making the program, I was afraid of leaving in the bit map table images that were no longer necessary every time Picture changed, making the document unnecessarily bigger and bigger.
The contents of an OpenDocument file can be extracted or viewed using a .zip archive utility. Assuming you have a zip archiving utility, the easiest way to see the contents of an OpenDocument file is to rename the OpenDocument file's extension to .zip and then double click it. The images are in the Pictures directory in the .zip file.
Back to top
View user's profile Send private message
ArielEnter
General User
General User


Joined: 11 Jun 2009
Posts: 25

PostPosted: Tue Nov 24, 2009 4:47 pm    Post subject: Conclusion Reply with quote

Once again, thanks James.

Well, my conclusion will be that OOo decides whether or not a picture is being used. If we delete all the records in the oBitmap variant the pictures are also deleted. But if a graphic object is also using one of the pictures, OOo doesn't delete it until this graphic object is deleted by the user. Well that solves everything.

I have tested already thanks to Jame's tip, and CangePicture (Mi macro) works perfectly without any waste of memory.

I'm so proud of the results, hope it helps a lot of people.
Back to top
View user's profile Send private message
ArielEnter
General User
General User


Joined: 11 Jun 2009
Posts: 25

PostPosted: Mon Aug 30, 2010 2:45 pm    Post subject: problems with Change picture Reply with quote

soundspaces said:
Quote:


hi, first of all thanks for yout work. It adds a feature to Open Office Writer very important and usefull to me.
I used it for a couple of days but today stopped to work suddently and apparently without reasons.

When I try to change the picture it returns and error that sounds like that:

"Select a picture to change. Unable to replace a picture"

It sounds very enigmatic to me.

Any help or suggestion will be appreciated!
Thanks again
SP


I'm very glad to have a feed back from my extension. Thank you very much.

About the problem you are having, well, the way I programed change picture, it can only identify one error, which happens when no image is being selected, if any other error happens then it tells you back the same message: "Select a picture to change. Unable to replace a picture".

have you experience this problem before? Could It have came with the new version of OOo? What version of OOo are you using? What OS are you using? Does it work in other machines? if it does, you may try to uninstall and install back OOo or the extension it self. Any clue you give me could help me to come up with an idea.

Mean while, I may further develop the code so that it gives back at least the error number and the system output. Maybe some one in the community will know what the problem is with that information.

Thanks soundspaces. Hope to hear from you.
Back to top
View user's profile Send private message
soundspaces
General User
General User


Joined: 27 Jul 2009
Posts: 7
Location: Italy

PostPosted: Mon Aug 30, 2010 10:24 pm    Post subject: Re: problems with Change picture Reply with quote

ArielEnter wrote:


I'm very glad to have a feed back from my extension. Thank you very much.

About the problem you are having, well, the way I programed change picture, it can only identify one error, which happens when no image is being selected, if any other error happens then it tells you back the same message: "Select a picture to change. Unable to replace a picture".

have you experience this problem before? Could It have came with the new version of OOo? What version of OOo are you using? What OS are you using? Does it work in other machines? if it does, you may try to uninstall and install back OOo or the extension it self. Any clue you give me could help me to come up with an idea.

Mean while, I may further develop the code so that it gives back at least the error number and the system output. Maybe some one in the community will know what the problem is with that information.

Thanks soundspaces. Hope to hear from you.



Hi, thanks for answer. Sorry, I forget all basic info to let you handle that feedback.
1) As I said I installed changepicture plugin and for a couple of day everythings was OK, but suddently it stopped to work giving me that error.
2) I had that problem with OOo 3.2 but today I installed 3.2.1 and reinstalled ChangePicture plugin but nothing changes
3) I'm using Win 7 x64
4) Sorry I did not try in other machines

thanks for your work, unfortunatelly I'm not able to manage code, so I won't be more usefull for developper's comunity.

Please let us know when you release a new version of your plugin.
I think it's very usefull for a lot of us.

Cheers!
SP
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group