[Android] Android. Graphics. Camera allows image rotation and scaling, and image skew with Matrix

Source: Internet
Author: User

Android. Graphics. camera can perform complex operations on images, such as rotating and blooming, and skews images with matrix.

In my summary, some differences between camera and matrix are as follows:

  1. The rotate () method of camera is to specify the Rotation Angle of a dimension.
  2. The rotate () method of matrix achieves the rotation of the specified angle clockwise. The rotation effect is the same as that of the specified Z axis of camera, but the opposite direction is true.
  3. The translate () method of camera scales the Image Based on the displacement of the viewpoint in a certain dimension. The effect is similar to that of the scale () method of matrix, but the scale () method of Matrix () you can directly specify the scaling ratio.
  4. Camera does not support tilting operations. matrix can directly perform tilting operations.

This article describes how to use camera. The implementation is as follows:

The processing result of camera is only a matrix. This matrix can be used to rotate or scale a bitmap in bitmap or canvas.

The Code is as follows:

Package lab. sodino. camera; import android. app. activity; import android. graphics. bitmap; import android. graphics. camera; import android. graphics. matrix; import android. graphics. drawable. bitmapdrawable; import android. OS. bundle; import android. util. log; import android. widget. imageview; import android. widget. seekbar; import android. widget. seekbar. onseekbarchangelistener; import android. widget. textview;/*** came Comparison between RA and matrix: <br/> * The rotate () method of camera specifies the Rotation Angle of a certain dimension. <Br/> * The rotate () method of matrix achieves the specified Rotation Angle clockwise. The rotation effect is the same as that of the Z axis specified by camera, but in the opposite direction. <Br/> ** the translate () method of camera scales the Image Based on the displacement of the viewpoint in a certain dimension, which is similar to the scale () method of matrix, * Only the scale () method of matrix is to directly specify the scale. <Br/> ** camera does not support tilting operations. matrix can directly perform tilting operations. <Br/> ** @ author sodino E-mail: sodinoopen@hotmail.com * @ version time: 04:17:49 */public class actcamera extends activity implements onseekbarchangelistener {private camera; // viewsprivate seekbar seekbarxrotate; private seekbar seekbaryrotate; private seekbar rotate; private textview txtxtxrotate; private textview txtyrotate; private textview txtzrtate; private seekbar seekbarx Skew; private seekbar member; private textview txtxtranslate; private textview txtytranslate; private textview txtztranslate; private imageview imgresult; // integer paramsprivate int rotatex, rotatey, rotatez; private float skewx, skewy; private int translatez;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {Super. oncreate (savedinstancestate); setcontentview (R. layout. main); // cameracamera = new camera (); // initviews // rotateseekbarxrotate = (seekbar) findviewbyid (R. id. seekbarxrotate); seekbarxrotate. setonseekbarchangelistener (this); seekbaryrotate = (seekbar) findviewbyid (R. id. seekbaryrotate); seekbaryrotate. setonseekbarchangelistener (this); seekbarzrtate = (seekbar) findviewbyid (R. id. seekbarzrtate); see Kbarzrtate. setonseekbarchangelistener (this); txtxrotate = (textview) returns xrotate); txtyrotate = (textview) returns yrotate); txtzrtate = (textview) returns rortate); // values = (seekbar) findviewbyid (R. id. seekbarxskew); seekbarxskew. setonseekbarchangelistener (this); seekbaryskew = (seekbar) findviewbyid (R. id. seekbaryskew); seekbaryskew. setonsee Kbarchangelistener (this); seekbarztranslate = (seekbar) findviewbyid (R. id. seekbarztranslate); seekbarztranslate. setonseekbarchangelistener (this); txtxtranslate = (textview) Export xskew); txtytranslate = (textview) findviewbyid(r.id.txt yskew); txtztranslate = (textview) Export ztranslate); imgresult = (imageview) findviewbyid (R. id. imgresult); // refreshrefreshimage ();} private Void refreshimage () {// obtain the image to be processed bitmapdrawable tmpbitdra = (bitmapdrawable) getresources (). getdrawable (R. drawable. honeycomb); bitmap tmpbit = tmpbitdra. getbitmap (); // start processing the image // 1. obtain the processing matrix // record the initial status. Save () and restore () can soften the image transition. // Each save shocould be balanced with a call to restore (). camera. save (); matrix = new matrix (); // rotatecamera. rotatex (rotatex); camera. rotatey (rotatey); camera. rotatez (rotatez); // translatecamera. translate (0, 0, translatez); camera. getmatrix (matrix); // restore to the initial state. Camera. restore (); // set the central point matrix for image processing. pretranslate (tmpbit. getwidth ()> 1, tmpbit. getheight ()> 1); matrix. preskew (skewx, skewy); // matrix. postskew (skewx, skewy); // setskew () directly, the rotate () and translate () processed earlier will be invalid. // Matrix. setskew (skewx, skewy); // 2. generate a new image (or act directly on the canvas) log through the matrix. D ("android_lab", "width =" + tmpbit. getwidth () + "Height =" + tmpbit. getheight (); bitmap newbit = NULL; try {// the width and height of the converted matrix image may not be greater than 0. In this case, illegalargumentexceptionnewbit = bitmap is thrown. createbitmap (tmpbit, 0, 0, tmpbit. getwidth (), tmpbit. getheight (), matrix, true);} catch (illegalargumentexception IAE) {IAE. printstacktrace ();} If (newbit! = NULL) {imgresult. setimagebitmap (newbit) ;}@ overridepublic void onprogresschanged (seekbar, int progress, Boolean fromuser) {If (seekbar = seekbarxrotate) {txtxrotate. settext (Progress + "progress"); rotatex = progress;} else if (seekbar = seekbaryrotate) {txtyrotate. settext (Progress + "progress"); rotatey = progress;} else if (seekbar = seekbarzrtate) {txtzrtate. settext (Progress + "progress"); rotatez = progress;} else if (seekbar = seekbarxskew) {skewx = (Progress-100) * 1.0f/100; txtxtranslate. settext (string. valueof (skewx);} else if (seekbar = seekbaryskew) {skewy = (Progress-100) * 1.0f/100; txtytranslate. settext (string. valueof (skewy);} else if (seekbar = seekbarztranslate) {translatez = progress-100; txtztranslate. settext (string. valueof (translatez);} refreshimage () ;}@ overridepublic void onstarttrackingtouch (seekbar) {}@ overridepublic void onstoptrackingtouch (seekbar ){}}

This article is owned by sodino, A csdn blog blogger.

Reprinted please indicate the source: http://blog.csdn.net/sodino/article/details/6823315

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.