An off-duty New York City police officer shot an armed man who opened fire on him during a suspected attempted carjacking early Wednesday morning in Brooklyn, officials said.

The officer, whose name and other information were not released, was on his way home in his car at the corner of East 87th Street and Foster Avenue in the Canarsie section of the borough around 2 a.m. when the 28-year-old suspect approached the vehicle and “knocked on his window with a firearm,” Chief of Detectives Rodney Harrison said during a news conference.

DEAD INFANTS WRAPPED IN PAPER FOUND DUMPED ON BRONX PATIO

Firearms recovered at scene of shooting on Nov. 11, 2020 (NYPD)

Firearms recovered at scene of shooting on Nov. 11, 2020 (NYPD)

The officer then pulled his car over and got out of the vehicle, at which point the gunman fired off one round, “barely missing” the cop, Harrison said. The officer fired back, striking the suspect in the chest.

Video of the alleged attack was shared on Twitter by local station News12 and shows the car come to a stop before a struggle ensues. 

STATEN ISLAND MAN ACCUSED OF MAKING DEATH THREATS AGAINST PROTESTERS, POLITICIANS, FBI

The wounded man was taken to an area hospital in serious condition. His name was not provided and charges against him were pending as of Wednesday morning.

The officer was taken to a different hospital to be treated for tinnitus, police said.

CLICK HERE TO GET THE FOX NEWS APP

Police had no reason to believe the suspect was intentionally targeting police, Harrison added.

Investigators recovered two guns and a knife at the scene.