A gunman is in custody after he ambushed police officers in the Bronx twice in 12 hours, wounding two in attacks.

The man, whose name was not immediately released, was captured after he walked into a police station and started shooting early on Sunday, hitting a lieutenant in the arm and narrowly missing other police personnel before he ran out of bullets, lay down and tossed his pistol.

That attack came just hours after the same man approached a patrol van in the same part of the Bronx and fired at two officers inside, wounding one.

Despite multiple shots fired in both incidents, nobody was killed and all are expected to recover, police said.

NYPD Officer Shot
The suspect is now in custody (NYPD/AP)

“It is only by the grace of God and the heroic actions of those inside the building that took him into custody that we are not talking about police officers murdered inside a New York City police precinct,” New York City Police Commissioner Dermot Shea said at a press conference Sunday.

He called the gunman a “coward” and said he had a lengthy criminal history, including a 2002 shooting and carjacking in which he also fired a gun at police officers.

Mr Shea said the man was paroled from prison in 2017 after an attempted murder conviction.

The commissioner also lashed out at criminal justice reform activists who have held demonstrations against excessive force by police in recent months, including a large protest in Grand Central Terminal.

He suggested the protests helped create an anti-police environment.

Mr Shea said: “These things are not unrelated. We had people marching through the streets of New York City recently.

“Words matter. And words affect people’s behaviour.”

Mr Shea did not offer any evidence the gunman in this weekend’s attacks knew of those protests or was influenced by them.

Mayor Bill de Blasio, who won office partly on a promise to reform overly aggressive policing of minority communities, also suggested that while people had a right to protest, anti-police sentiment had got out of hand.

“This was an attempt to assassinate police officers,” he said.

“We need to use that word.”

Two security camera videos, posted on social media, captured the shooting inside the headquarters of the 41st Precinct, which happened shortly before 8am.

In one of them, the gunman is seen sauntering into the precinct lobby, before briefly disappearing off screen.

Police said the lieutenant who was shot returned fire but did not hit the gunman.

Then, the gunman rushed into a side room and fired at two people there, including a civilian employee, as they fled.

He then retreated to the lobby and dived on to the floor.

In another video from a different angle, an officer in the precinct lobby is seen reacting to the first gunshot.

Officers converge, pointing their guns, and the gunman’s pistol is seen sliding away from him across the floor.

NYPD Officer Shot
The police van with a bullet hole above the door handle (NYPD/AP)

The shooting inside the precinct headquarters came just hours after another attack in the same section of the Bronx, involving the same suspect.

Two officers narrowly escaped with their lives when a gunman fired into their patrol van just before 8.30pm on Saturday.

The two uniformed officers, partners for eight years and friends since middle school, were sitting in their van with emergency lights activated when a man approached them and engaged them in conversation, Mr Shea said.

The man asked the officers for directions, then pulled out a gun “without provocation”, the commissioner said.

The man fired multiple shots, grazing the officer behind the wheel in the chin and neck, and narrowly missing an artery.

Neither officer returned fire. The officer’s partner drove him to a hospital nearby.

On Sunday afternoon, he was released from the hospital to resounding cheers and applause from a sizeable contingent of fellow officers.

The officer, bandage visible on his neck, gave a thumbs-up to the crowd before getting into a waiting car.

“He is lucky to be alive,” Mr Shea said. “He is expected to make a full recovery and it is a miracle.”

New York Governor Andrew Cuomo said in a tweet Sunday he was “horrified by the multiple attacks” on police.

He said: “NY’s law enforcement officers put their lives on the line every day to keep us safe. These attacks are heinous.”

US President Donald Trump immediately used the shootings to assail New York’s Democratic mayor and governor.

“I grew up in New York City and, over many years, got to watch how GREAT NYC’s ‘Finest’ are,” he said.

“Now, because of weak leadership at Governor & Mayor, stand away (water thrown at them) regulations, and lack of support, our wonderful NYC police are under assault. Stop this now!” he tweeted.