New York City police officers work the scene of a police-involved shooting outside the 41st precinct Sunday in New York. Credit: John Minchillo | AP

NEW YORK — A gunman is in custody after he ambushed police officers in the Bronx twice in 12 hours, wounding two in attacks that ignited outrage from officials who blamed the violence on an atmosphere of anti-police rhetoric.

The man, whose name was not immediately released, was captured after he walked into a police station in the Bronx and started shooting shortly before 8 a.m. Sunday.

Two security camera videos posted on social media captured the shooting inside the 41st Precinct, which left one officer with injuries to his arm. One of the videos shows the suspect firing at police personnel before he ran out of bullets, lay down and tossed his pistol on the floor.

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, police said.

On Saturday evening, police say the suspect walked up to the van asking the officers for directions and then fired multiple shots, grazing the officer behind the wheel in the chin and neck, and narrowly missing an artery.

Neither officer returned fire.

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

“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.

The officer injured in the first shooting was released from the hospital Sunday afternoon 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.

Shea 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. 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 that the protests helped create an anti-police environment.

“These things are not unrelated. We had people marching through the streets of New York City recently,” Shea said. “Words matter. And words affect people’s behavior.”

Shea didn’t offer any evidence that 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 police had a right to protest, anti-police sentiment had gotten out of hand.

“Anyone who spews hatred at our officers is aiding and abetting this kind of atmosphere, it is not acceptable,” de Blasio said. “You could protest for whatever you believe in but you cannot vilely attack those who are here to protect us. It creates this kind of dynamic.”

Robert Gangi, executive director of the Police Reform Organizing Project advocacy group, pushed back against the criticism aimed at protests, and said it was “irresponsible” for Shea and de Blasio to say the shooter’s behavior “is a result of the demonstrations and protestors who are protesting in a legitimate fashion.”

Of the shooter, Gangi said there is “no defense for a lunatic who opens fire on police.”

The attacks recalled other unprovoked assaults on police officers sitting in their patrol vehicles.

In 2017, a gunman killed Officer Miosotis Familia as she sat in her patrol vehicle in the Bronx. In 2014, two officers, Rafael Ramos and Wenjian Liu, were shot dead in their patrol car in Brooklyn by a man upset about recent police killings of unarmed black men.

The killings of Ramos and Liu had also followed large street protests and some officers blamed de Blasio for expressing solidarity with the demonstrations, and turned their backs on the Democrat at the funerals.

New York Gov. Andrew Cuomo said in a tweet Sunday he was “horrified by the multiple attacks” on police. “NY’s law enforcement officers put their lives on the line every day to keep us safe. These attacks are heinous.”

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. 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.

The attacks happened in the Bronx’s 41st Precinct, a once crime-plagued district whose former headquarters was infamously branded “Fort Apache,” and was the subject of a 1981 film starring Paul Newman.

In recent years, though, the neighborhood has gotten much safer. There were five killings reported in all of last year and 164 robberies, down from 44 killings and 1,095 robberies in 1990.