A Cook County Sheriff’s correctional officer accused of punching a detainee was released on his own recognizance Thursday after a judge watched videos of the beating and said it could be argued that the inmate appeared to be resisting the officer and his colleagues.

Miguel Ortiz is neither a flight risk nor a danger to society, Cook County Judge James Brown said, questioning why authorities took 2 1/2 years to charge Ortiz with official misconduct.

Prosecutor Ahmed Kosoko told Brown that the Cook County state’s attorney’s office wasn’t notified of the Jan. 17, 2014, incident until a year ago and that the matter was part of a “larger investigation.”

Ortiz, a 20-year veteran of the sheriff’s office, was also charged with misdemeanor battery. He would lose his pension if convicted of official misconduct, which is a felony.

On the night in question, the inmate refused to get into a cell in a medium-security wing of the Cook County Jail after he was transferred from another wing, Kosoko said. The inmate had just overheard officers discussing that the cell was used to quarantine a sick inmate, prosecutors said.

The man dropped the blankets he was carrying on the floor and said he was not going into a cell that “had quarantine,” Kosoko said.

One of the correctional officers told the man to “relax” as they were working on the issue, Kosoko said.

Ortiz, who was in a nearby control room, overheard the inmate’s complaints, saw him drop the blankets and asked the man if he was refusing to go into the cell. The inmate confirmed he didn’t want to go in that cell and asked for a supervisor, Kosoko said.

As he stood with his arms crossed, Ortiz grabbed the man and he fell backwards.

Kosoko said the inmate fell to the ground after tripping over his own feet as Ortiz tried to reach for his arm to escort him to the cell.

One of the other officers then went down on the ground to put the man in handcuffs. It was then that Ortiz knelt down and punched the inmate seven times in the face and head, Kosoko said.

The video showed the man did nothing to antagonize Ortiz, sheriff’s investigators said.

The video contradicted Ortiz’s statement that the detainee took a fighting stance before he was struck, sheriff’s investigators said, determining that Ortiz used excessive force.

Brown on Thursday used Kosoko’s laptop computer to watch videos taken by two cameras in the jail. He narrated what was taking place on the screen for courtroom spectators.

The judge said he did see Ortiz throw some punches but he also added that the inmate appeared to be “flailing his legs” as correctional officers tried to “restrain” him.

The inmate was taken to the hospital at the Cook County Jail. His eye was swollen, but he wasn’t seriously injured, officials said. He was treated for blunt trauma to the head, Kosoko said.

The next day, he signed a complaint, seeking Ortiz’ firing for “beating me in the face and for banging my head against the ground.”

The Cook County Sheriff’s office moved to fire Ortiz on March 11, four months after investigators found he used excessive force. He’s on unpaid leave while his case is pending before the independent Merit Board, which decides discipline for sheriff’s employees.

On Wednesday, the Cook County State’s Attorney’s office approved criminal charges against the 44-year-old Ortiz, who turned himself in Thursday morning.

Ortiz has been married for two decades and has two children, ages 15 and 19, an assistant public defender said. The Roosevelt High School graduate also served as a reservist in the Marines.

Cook County Sheriff Tom Dart posted the videos of the Ortiz beating on the office’s web site as part of an effort to boost transparency about what happens in the jail, said Ben Breit, a spokesman for the office.

In April, the office released six videos involving 13 officers. Six of them have been fired for misconduct, Breit said. The sheriff sought to fire a seventh officer, but the Merit Board reduced his punishment to a 180-day suspension. Three more officers have been fired since then.

In June, the office released four more videos showing inmates attacking correctional officers. Several of the attacks landed the officers in the hospital. From January 2015 to the end of June, investigations have led to criminal charges against more than 160 inmates for aggravated battery to a police officer.

It’s unusual for a correctional officer to be fired — or face criminal charges. More than 1,100 excessive force complaints have been filed against jail employees over the past seven years. Nine percent of the complaints were sustained, and 4 percent of the complaints resulted in suspensions of a day to a month, the Better Government Association reported in May. A tiny fraction resulted in firings or charges.

The Ortiz beating was captured on two video cameras, which are among 2,500 fixed cameras in the jail, officials said. Correctional officers also wear body cameras and use handheld cameras to document tense situations, Breit said.