How to customize the display of pictures, custom display of pictures
1. Customize camera preview and take photos
Package com. android. myapplication; import android. content. contentValues; import android. content. res. configuration; import android. hardware. camera; import android.net. uri; import android. OS. bundle; import android. provider. mediaStore; import android. support. v7.app. appCompatActivity; import android. util. log; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. view; import andro Id. view. window; import android. widget. toast; import java. io. fileNotFoundException; import java. io. IOException; import java. io. outputStream; import java. util. list; public class CameraActivity extends AppCompatActivity implements SurfaceHolder. callback, Camera. pictureCallback {// SurfaceView size in xml private static final int LARGEST_WIDTH = 200; private static final int LARGEST_HEIGHT = 200; private android. View. surfaceView svCamera; private Camera camera; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); supportRequestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_camera); initView (); initData ();} private void initData () {SurfaceHolder holder = svCamera. getHolder (); holder. addCallback (this);} private void initView () {svC Amera = (SurfaceView) findViewById (R. id. sv_camera) ;}@ Override public void surfaceCreated (SurfaceHolder holder) {int numberOfCameras = Camera. getNumberOfCameras (); Camera. cameraInfo cameraInfo = new Camera. cameraInfo (); // test code to check the number of cameras available if (numberOfCameras> 1) {for (int I = 0; I <numberofcameras; I ++) {camera. getcamerainfo (I, camerainfo); = "" if (camerainfo. facing = "=" camera. camerainfo. camera_facing_back) {= "" Log. e ("=" = & quot;, & quot; Post & quot;); "} else =" "camera. camerainfo. camera_facing_front) {= "" {= ""} = "" try = "" after the above query, it is found that the front and back judgments of different devices are inaccurate, for example, the front camera id of our device is 0 rather than 1 = "" Camera = "camera. open (0); "The system default Preview mode is landscape, vertical preview requires rotation angle =" "camera. parameters = "" parameters = "camera. getParameters (); "if (getresources (). getconfiguration (). orientation! = "Configuration. ORIENTATION_LANDSCAPE) {// portrait screen "parameters. set ("orientation", "portrait"); = "" camera. setdisplayorientation (90); = "" parameters. setrotation (90); = ""} else {= "" horizontal screen = "" parameters. set ("orientation", "landscape"); = "" camera. setdisplayorientation (0); = "" parameters. setrotation (0); = "" add special effect = "" list = "" supportedcoloreffects = "parameters. getSupportedColorEffects (); "for =" "(string =" "currenteffect =" ": = "" Supportedcoloreffects) = "" query whether related effects are supported = "if =" "(camera. parameters. effect_mono.equals (currenteffect) {= "" parameters. setcoloreffect (currenteffect); = "" break; = "" Change camera preview size = "" first query the list of all sizes supported by the device = "" int = "" bestwidth = "0; "bestheight =" 0; "list supportedPictureSizes = parameters. getSupportedPictureSizes (); if (supportedPictureSizes. size ()> 1) {for (Camera. size aSize: supportedPictureSizes) {// check whether the specified Size meets the requirements of the device. Dimension conditions: the supported dimensions are greater than 0 and smaller than or equal to our custom dimensions. if (aSize. width> bestWidth & aSize. width <= LARGEST_WIDTH & aSize. height> bestHeight & aSize. height <= LARGEST_HEIGHT) {bestWidth = aSize. width; bestHeight = aSize. height; break ;}} if (bestWidth! = 0 & bestHeight! = 0) {parameters. setPreviewSize (bestWidth, bestHeight) ;}} camera. setParameters (parameters); camera. setPreviewDisplay (holder); camera. startPreview ();} catch (IOException e) {e. printStackTrace (); camera. release () ;}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}@ Override public void surfaceDestroyed (SurfaceHolder holder) {camera. stopPreview (); Camera. release ();} public void take_photo (View view) {if (camera! = Null) {camera. takePicture (null, null, this) ;}@ Override public void onPictureTaken (byte [] data, Camera camera) {Uri imgFileUri = getContentResolver (). insert (MediaStore. images. media. EXTERNAL_CONTENT_URI, new ContentValues (); try {OutputStream imgFileOS = getContentResolver (). openOutputStream (imgFileUri); imgFileOS. write (data); imgFileOS. flush (); imgFileOS. close (); Toast. makeText (this, "the image has been saved! ", Toast. LENGTH_SHORT ). show (); camera. startPreview ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} xml: