What a joker!

Actor Ben Affleck was banned for life from playing blackjack at the Hard Rock Cafe in Vegas ­after security officers busted him counting cards, a source told The Post Friday.

The Oscar-winning star had come to Sin City with his wife, actress Jennifer Garner, to spend some time together before he ­begins shooting a new “Batman” movie, where he plays the caped superhero.

“Ben was told that he cannot ever play blackjack at the Hard Rock again,’’ the source said.

“He was there with Jen on Tuesday night for a romantic break before he headed off to Detroit to shoot ‘Batman,’ ” the source said.

“He was playing blackjack at a high-rollers table on Tuesday and was doing pretty well. He is really smart and a great blackjack player.

“Suddenly, managers approached Ben and told him. ‘You are playing too well. You are going to have to stop playing blackjack. You can play any other game at the Hard Rock, but you are banned from playing blackjack in our casino.’ ’’

The source stressed, “What he was doing isn’t ­illegal. He’s basically an ‘advantaged player’ because he knows what he’s doing.

“He and Jen decided to leave, and security arranged for a car service to take them back to their hotel.”

Although he’s down for the “count,’’ the hotel told him he’s welcome back at the casino any time — as long as he steers clear of the blackjack table.

Besides being a gifted actor, director and screenwriter, Affleck is also an extremely talented card player.

He raked in $800,000 in blackjack winnings at the Hard Rock by playing three $20,000 hands in one night in 2001.

He recently joined his buddy Matt Damon to participate in the World ­Series of Poker.

It’s not like he needed the winnings.

The Web site “Celebrity Net Worth’’ says Affleck has about $75 million.

Counting cards is a gambling strategy that decreases betting risk in blackjack. It isn’t illegal but many casinos forbid it, and it’s grounds for getting kicked out.

It involves remembering which cards have already been played to determine the odds of whether or not to place a high bet.

A spokesman for Affleck declined comment.