Jump to content

Need help to save player's screenshot.


Salem

Recommended Posts

Hi. I try to save screenshot on disk taken by player. Following the only wiki help I have below functions.

on client:
 

addEventHandler ( "onClientPlayerWeaponFire", getRootElement(), function( weapon, _, _, _, _, _, element )
    if ( weapon == 43 ) then -- camera
            triggerServerEvent( "sendScreenShot", getLocalPlayer(), scrX, scrY );
    end

end );

it's ok, sends data to the server, but:
 

addEvent( "sendScreenShot", true )
addEventHandler ( "sendScreenShot", getRootElement(), function( scrX, scrY )
    local name = getPlayerName( source )    
    takePlayerScreenShot( source, scrX, scrY, name, 30 );
    outputDebugString( "image taken by "..name.." of "..scrX.."x"..scrY )
end );    

addEvent( "OnPlayerScreenShot", true )
addEventHandler ( "OnPlayerScreenShot", getRootElement(), function( theResource, status, pixels, timestamp, tag )
    if status == "ok" then
        outputDebugString( "image taken by "..tag )
        outputDebugString( "at "..tostring(timestamp).." with "..pixels.." size" )
        local name = tag.."_"..timestamp;
    else
        outputDebugString( "image failed by "..tag.." at "..tostring(ts) )
    end
end );

And everything passes ok. Now I have some data and the image (I guess). My question is: how to save it on disk as JPEG? What functions should I use?

I guess, I need to use

FileCreate and dxDrawImage but how to use it together?


 
Edited by Salem
Link to comment

I'm trying to save it directly:

addEvent( "onPlayerScreenShot", true )
addEventHandler ( "onPlayerScreenShot", getRootElement(), function( theResource, status, pixels, timestamp, tag )
    if status == "ok" then        
        local iname = tag.."_"..getRealDateTimeNowString();
        outputDebugString( "[IMAGE] "..iname )
        local newFile = fileCreate( "e:/"..iname..".jpg" )                -- attempt to create a new file
            if ( newFile ) then                                       -- check if the creation succeeded
                fileWrite( newFile, "This is a test file!" )        -- write a text line
                fileClose( newFile )                                -- close the file once you're done with it
                outputDebugString( "[IMAGE] saved." )
            else
                outputDebugString( "[IMAGE] not saved." )            
            end
    else
        outputDebugString( "Image failed by "..tag.." at "..getRealDateTimeNowString() )
    end
end );


but error 'image not saved' appears.

Link to comment

At first glance I saw that you've made a typo

OnPlayerScreenShot

should be

onPlayerScreenShot

The wiki says

  • imageData: A string which contains the JPEG image data. This can be saved with the file functions, or sent to players with triggerClientEvent or even uploaded to a web site.

But it's pretty weird that there isn't any mention of saving a picture in none of the file functions. All of the parameters required are strings so putting the image data instead won't work. I'd also like to know the answer to this since it's something I've never even thought of doing. :)

 

5 minutes ago, Salem said:

I'm trying to save it directly:


addEvent( "onPlayerScreenShot", true )
addEventHandler ( "onPlayerScreenShot", getRootElement(), function( theResource, status, pixels, timestamp, tag )
    if status == "ok" then        
        local iname = tag.."_"..getRealDateTimeNowString();
        outputDebugString( "[IMAGE] "..iname )
        local newFile = fileCreate( "e:/"..iname..".jpg" )                -- attempt to create a new file
            if ( newFile ) then                                       -- check if the creation succeeded
                fileWrite( newFile, "This is a test file!" )        -- write a text line
                fileClose( newFile )                                -- close the file once you're done with it
                outputDebugString( "[IMAGE] saved." )
            else
                outputDebugString( "[IMAGE] not saved." )            
            end
    else
        outputDebugString( "Image failed by "..tag.." at "..getRealDateTimeNowString() )
    end
end );


but error 'image not saved' appears.

Try without the "e:/" part in fileCreate:

local newFile = fileCreate(iname..".jpg")

But fileWrite's second argument must be a string so I can't see how you'll put the image data there.

Edited by koragg
Link to comment

Thanks for replying. Honestly, my last solution works. The only error was (which I didn't visualize) in

getRealDateTimeNowString

function. It had : characters in string. Now it's ok. I've corrected typo and changed

fileWrite( newFile, pixels )   

The file is written to local resource.

local newFile = fileCreate( iname..".jpg" )

Now, the problem is the jpegs are horrible comparing with manual screenshots. The camerea targets are on and flash glows the screen. It seems like trigger is too late. I can't set up timer with negative values.

Link to comment

So I guess this starting event doesn't work properly. I think I haven't got any instead. However, On wiki is written:

OnClientPlayerWeaponFire

 

 

This event is called when a player fires a weapon. This does not trigger for projectiles, melee weapons, or camera.

But in my case it triggers the camera but probably after the clip. So that's why the images are not sharp. Any ideas?

Link to comment

Thanks for this suggestion - it's probably the only way to do this, so I did as follows:

function to check is target on via camera:

function isTargetingActivated( )    
    local slot = getPedWeaponSlot( me )
    if getPedWeapon( me, slot ) == 43 then
        outputDebugString( "you have camera dude" )
        return true
    else
        outputDebugString( "wrong tool dude" )
        return false
    end        
end
addEventHandler( "onClientPlayerTarget", getRootElement(), isTargetingActivated )


and to release the cilium:

function tryMakePhoto( button, press )    
    if ( button == "mouse1" ) then        
        if ( press ) then
            outputChatBox( " Set proper cilium to make well photographs.", 100, 160, 200 )
            --sns = getTickCount()
        else -- when released        
            if photojob == true then
                if isTargetingActivated( ) then
                    outputDebugString( " Target Activated." )
                    triggerServerEvent( "sendScreenShot", getLocalPlayer(), scrX, scrY );
                    --local cilium = sns - getTickCount()
                    outputChatBox( " You made a photo. View gallery how it's like.", 100, 120, 200 )                
                else
                    outputDebugString( " Target de-Activated." )                            
                end
            end    
        end    
    end    
end
addEventHandler( "onClientKey", getRootElement(), tryMakePhoto )

Photos are much better. You decide when to release the "bird" but still a major issue remains: how to get rid of camera target on its lens?? If I press fire button it disappears but is still on photos. Any ideas?

It's funny, because if you release target button before the cilium you got selfie (from behind) =)

 

Edited by Salem
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...