摄像头视图SurfaceView存储在风景只 [英] Camera View SurfaceView Stored in Landscape only

查看:317
本文介绍了摄像头视图SurfaceView存储在风景只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android和使用surfaceview overrelay框架的思想努力摄像头,发现这个例子.. <一个href=\"http://androidbyhp.blogspot.in/2011/07/android-creating-and-saving-overlay-on.html?showComment=1458304707072#c7292258410987461915\"相对=nofollow> Android的创造摄像头和overrelay框架。例如是伟大的。

1.Started提高所改变potrait和景观框架,摄影机视图不显示当我改变,从这个链接potrait所以检查,并得到了ANS <一个href=\"http://stackoverflow.com/questions/3841122/android-camera-$p$pview-is-sideways/5110406#5110406\">stackoverflow(changing显示方向),插入$在面c $ C改变为setparameters that..showed错误,所以删除了,它的工作

2.But当我看到,我把它显示这样的照片..结果
<一href=\"https://lh3.googleusercontent.com/QtgXkusOZubEwbTO2oqWt53aGPNS7iRuj_-SNcV-8Vyb3Tlxe2tS6ViSeQ8RxS2QF5SvPSZ-j9VWfnhZyXKGF1k-raS9=s192\"相对=nofollow>图片。话又说回来了ANS从这个链接<一个href=\"http://stackoverflow.com/questions/13245556/exif-orientation-tag-value-always-0-for-image-taken-with-portrait-camera-app-and\">ExifInterface(orientatioin)

<醇开始=3>
  • 前置摄像头无法打​​开,太..?

  • cambutton.xml

     &LT;?XML版本=1.0编码=UTF-8&GT?;
    &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;
    &LT;的ImageButton
    机器人:ID =@ + ID / btnCapture
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =500dp
    机器人:背景=@绘制/ CAM
    机器人:layout_centerHorizo​​ntal =真
    /&GT;
    &LT;的ImageButton
        机器人:ID =@ + ID / btnswitch
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@绘制/ switchcamera
        机器人:layout_marginLeft =85dp
        机器人:layout_marginStart =85dp
        机器人:layout_marginTop =500dp
        机器人:layout_toRightOf =@ ID / btnCapture
        机器人:layout_toEndOf =@ ID / btnCapture
        /&GT;&LT; / RelativeLayout的&GT;

    campotrait.xml

     &LT;?XML版本=1.0编码=UTF-8&GT?;
    &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    &GT;&LT; SurfaceView
        机器人:ID =@ + ID / cameraSurfaceView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=@绘制/帧3
        /&GT;
    &LT; / LinearLayout中&GT;

    camoverrelay1.xml是风景,同样的code以上。

    Share.java

     进口android.app.Activity;
    进口android.content.Intent;
    进口android.content.pm.PackageManager;
    进口android.content.res.Configuration;
    进口android.graphics.Bitmap;
    进口android.graphics.BitmapFactory;
    进口android.graphics.Canvas;
    进口android.graphics.PixelFormat;
    进口android.graphics.drawable.Drawable;
    进口android.hardware.Camera;
    进口android.os.Bundle;
    进口android.os.Environment;
    进口android.util.Log;
    进口android.view.Display;
    进口android.view.LayoutInflater;
    进口android.view.Surface;
    进口android.view.SurfaceHolder;
    进口android.view.SurfaceView;
    进口android.view.View;
    进口android.view.Window;
    进口android.view.WindowManager;
    进口android.widget.ImageButton;
    进口android.widget.RelativeLayout;
    进口android.widget.Toast;进口java.io.ByteArrayOutputStream中;
    进口的java.io.File;
    进口java.io.FileOutputStream中;
    进口java.io.IOException异常;
    进口的java.util.List;
    公共类股扩展活动实现SurfaceHolder.Callback {私人摄像头摄像头= NULL;
    私人SurfaceView cameraSurfaceView = NULL;
    私人SurfaceHolder cameraSurfaceHolder = NULL;
    私人布尔previewing = FALSE;私人显示器显示= NULL;私有静态诠释WID = 0,HGT = 0;私人LayoutInflater layoutInflater = NULL;
    私人查看cameraViewControl = NULL;
    私人RelativeLayout.LayoutParams layoutParamsControl = NULL;私人的ImageButton btnCapture = NULL;
    私人的ImageButton btnswitch = NULL;
    Camera.Size previewSize = NULL;
    Camera.Size optimalSize = NULL;
    位图mBitmap;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);    显示= getWindowManager()getDefaultDisplay()。
        WID = display.getWidth();
        HGT = display.getHeight();    。getWindow()和setFormat(PixelFormat.TRANSLUCENT);    requestWindowFeature(Window.FEATURE_NO_TITLE);
     getWindow()。setFlags(WindowManager.LayoutParams。
      FLAG_FULLSCREEN,窗口管理器。
      LayoutParams.FLAG_FULLSCREEN);
        。INT方向= getResources()getConfiguration()方向。
      / * Camera.Parameters p值= camera.getParameters();
        P.SET(方向,肖像);
        P.SET(旋转,90);
        camera.setParameters(P); * /
        如果((取向== Configuration.ORIENTATION_PORTRAIT)){
         / * camera.setDisplayOrientation(90); * /
            的setContentView(R.layout.campotrait);
        }
        其他{
           / * camera.setDisplayOrientation(180); * /
            的setContentView(R.layout.cameraoverlay1);
        }    cameraSurfaceView =(SurfaceView)findViewById(R.id.cameraSurfaceView);
        cameraSurfaceHolder = cameraSurfaceView.getHolder();
        cameraSurfaceHolder.addCallback(本);
        cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    layoutInflater = LayoutInflater.from(getBaseContext());
        layoutParamsControl =新RelativeLayout.LayoutParams
     (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams。
     FILL_PARENT);    cameraViewControl = layoutInflater.inflate(R.layout.cambutton,NULL);
        this.addContentView(cameraViewControl,layoutParamsControl);
        btnCapture =(的ImageButton)findViewById(R.id.btnCapture);
        btnswitch =(的ImageButton)findViewById(R.id.btnswitch);
        btnCapture.setOnClickListener(新View.OnClickListener()
        {
            @覆盖
            公共无效的onClick(视图v)
            {
                // TODO自动生成方法存根
                camera.takePicture(cameraShutterCallback,
                        cameraPictureCallbackRaw,
                        cameraPictureCallbackJpeg);        }
        });
        btnswitch.setOnClickListener(新View.OnClickListener()
        {
            @覆盖
            公共无效的onClick(视图v)
            {
                // TODO自动生成方法存根
                INT camerasNumber = Camera.getNumberOfCameras();
                如果(camerasNumber→1){
                    //释放旧相机实例
                    //开关相机,从正面和背面,反之亦然
                    openFrontFacingCameraGingerbread();
                }其他{
     吐司面包= Toast.makeText(getApplicationContext(),对不起,您的
     手机只有一个摄像头,Toast.LENGTH_LONG)!;
                    toast.show();
                }
            }
        });
     }
     私人相机openFrontFacingCameraGingerbread(){
        INT cameraCount = 0;
        相机凸轮=无效;
        Camera.CameraInfo cameraInfo =新Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        对(INT camIdx = 0; camIdx&下; cameraCount; camIdx ++){
            Camera.getCameraInfo(camIdx,cameraInfo);
            如果(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
                尝试{
                    凸轮= Camera.open(camIdx);
                }赶上(RuntimeException的E){
                    e.printStackTrace();
     Toast.makeText(getApplicationContext(),无法打开
      前置摄像头,Toast.LENGTH_SHORT).show();
                }
            }
        }    返回凸轮;
     }
    Camera.ShutterCallback cameraShutterCallback =新Camera.ShutterCallback()
    {
        @覆盖
        公共无效onShutter()
        {    }
     }; Camera.PictureCallback cameraPictureCallbackRaw =
     新Camera.PictureCallback()
    {
        @覆盖
        公共无效onPictureTaken(字节[]数据,相机摄像头)
        {    }
    };Camera.PictureCallback cameraPictureCallbackJpeg =
    新Camera.PictureCallback()
    {
        @覆盖
        公共无效onPictureTaken(字节[]数据,相机摄像头)
        {
            // TODO自动生成方法存根
    位图cameraBitmap = BitmapFactory.de codeByteArray的(数据,0,data.length);        WID = cameraBitmap.getWidth();
            HGT = cameraBitmap.getHeight();
    位图newImage = Bitmap.createBitmap(WID,HGT,Bitmap.Config.ARGB_8888);        帆布帆布=新的Canvas(newImage);
            canvas.drawBitmap(cameraBitmap,0F,0F,NULL);
            。INT方向= getResources()getConfiguration()方向。        如果((取向== Configuration.ORIENTATION_PORTRAIT)){
                可绘制可绘制= getResources()。getDrawable
                        (R.drawable.potraitimage);
                断言绘制!= NULL;
                drawable.setBounds(0,0,WID,HGT);
                drawable.draw(画布);        }
            其他{
                可绘制可绘制= getResources()。getDrawable
                        (R.drawable.curtain);
                断言绘制!= NULL;
                drawable.setBounds(0,0,WID,HGT);
                drawable.draw(画布);        }
         / *位图bitm = NULL; * /
            文件storagePath =新的文件(环境。
                    getExternalStorageDirectory()+/ ActEventz /);
            storagePath.mkdirs();        文件MYIMAGE =新的文件(storagePath,test.jpg放在);
     ExifInterface EXIF​​;
            尝试{
                EXIF =新ExifInterface(storagePath +test.jpg放在);
                INT东方= exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,0);
                Log.d(EXIF,的Exif:+东方);
                字模=新的Matrix();
                如果(东方== 0){
                    matrix.postRotate(0);
                    Log.d(EXIF,的Exif:+东方);
                }如果别人(东方== 90){
                    matrix.postRotate(0);
                    Log.d(EXIF,的Exif:+东方);
                }
                mBitmap = Bitmap.createBitmap(cameraBitmap,0,
       0,cameraBitmap.getWidth(),cameraBitmap.getHeight(),矩阵,真);
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            ByteArrayOutputStream流=新ByteArrayOutputStream();
             mBitmap.com preSS(Bitmap.Com pressFormat.PNG,0,流);
            字节[]的字节数组= stream.toByteArray();
            意图I =新意图(getApplicationContext(),Imageview.class);
            捆绑BU =新包();
            bu.putByteArray(照片,字节);
            i.putExtras(BU);
            startActivity(ⅰ);
            尝试
            {
                FileOutputStream中出=新的FileOutputStream(MYIMAGE);
                newImage.com preSS(Bitmap.Com pressFormat.JPEG,80出);
       Toast.makeText(getApplicationContext(),图像捕获
       Toast.LENGTH_SHORT).show();
                了out.flush();
                out.close();
            }赶上(IOException异常E)
            {
                Log.d(在保存文件E +);
            }
          / *矩阵矩阵=新的Matrix();
            matrix.postRotate(90);
            位图位图= BitmapFactory.de codeFILE(storagePath +test.jpg放在);
    位图rotatedBitmap = Bitmap.createBitmap(位图,0,0,bitmap.getWidth(),bitmap.getHeight(),矩阵,真);
    位图scaledBitmap = Bitmap.createScaledBitmap(rotatedBitmap,
     HGT,妇女参与发展,真正的);        scaledBitmap.com preSS(Bitmap.Com pressFormat.PNG,0,流); * /        camera.start preVIEW();
            newImage.recycle();
            newImage = NULL;
            cameraBitmap.recycle();
            cameraBitmap = NULL;
        }
    };
    @覆盖
    公共无效onRequestPermissionsResult(INT申请code,
    字符串权限[],INT [] grantResults){
        开关(要求code){
            案例200:{
                //如果请求被取消,结果数组是空的。
    如果(grantResults.length大于0&放大器;&放大器; grantResults [0] ==软件包管理系统。
     许可授予) {
                    //许可授予
                }其他{
                    //没有权限
                }
                返回;
      //使用其他线路的情况下其他请求(具有不同的要求codeS)
            }
        }
     }
     @覆盖
     公共无效surfaceChanged(SurfaceHolder持有人,
                               INT格式,诠释的宽度,高度INT)
     {
        // TODO自动生成方法存根    如果(previewing)
        {
            camera.stop preVIEW();
            previewing = FALSE;
        }        Camera.Parameters参数= camera.getParameters();
     显示显示=((窗口管理器)getSystemService
     (WINDOW_SERVICE))getDefaultDisplay()。
     清单&LT; Camera.Size&GT; previewSizes = parameters.getSupported previewSizes();
             previewSize = previewSizes.get(0);     //你需要选择你的应用程序最合适的previewSize
         // ....选择previewSizes之一,在这里
     / * parameters.set previewSize(previewSize.width,previewSize.height); * /
           如果(display.getRotation()== Surface.ROTATION_0)
            {
                parameters.set previewSize(previewSize.height,previewSize.width);
                camera.setDisplayOrientation(90);        }        如果(display.getRotation()== Surface.ROTATION_90)
            {
                parameters.set previewSize(previewSize.width,previewSize.height);        }        如果(display.getRotation()== Surface.ROTATION_180)
            {
                parameters.set previewSize(previewSize.height,previewSize.width);
            }        如果(display.getRotation()== Surface.ROTATION_270)
            {
                parameters.set previewSize(previewSize.width,previewSize.height);
                camera.setDisplayOrientation(180);        }
           /*camera.setParameters(parameters);*/
        //这里发生错误setParameters失败
        尝试
        {
                camera.set previewDisplay(cameraSurfaceHolder);
                camera.start preVIEW();
                previewing = TRUE;
            }
            赶上(IOException异常E)
            {
                // TODO自动生成catch块
                e.printStackTrace();
            }
      }
    @覆盖
    公共无效surfaceCreated(SurfaceHolder持有人)
    {
        // TODO自动生成方法存根
        尝试
        {
            相机= Camera.open();
        }
        赶上(例外五)
        {
     Toast.makeText(getApplicationContext(),设备相机不
    。正常工作,请稍后再试,Toast.LENGTH_LONG).show();
        }
    }@覆盖
    公共无效surfaceDestroyed(SurfaceHolder持有人)
    {
        // TODO自动生成方法存根
        camera.stop preVIEW();
        camera.release();
        摄像头= NULL;
        previewing = FALSE;
    }
    }

    我已搜查很多网站,计算器,我不能得到正确的答案,唯一的错误是相机的拍摄观点,请给我建议的解决方案家伙..我停留在这整整一天..谢谢你在前进。


    解决方案

    感谢您的支持家伙..我已经只是改变了一些codeS在表面改解决了这个问题。我已删除了部分ExifInterface本身。 。正下方的编码做过surfacechanged图像保存为预期..:)

      @覆盖
    公共无效surfaceChanged(SurfaceHolder持有人,
                       INT格式,诠释的宽度,高度INT)
    {
    // TODO自动生成方法存根如果(previewing)
    {
    camera.stop preVIEW();
    previewing = FALSE;
    }
    Camera.Parameters参数= camera.getParameters();
      显示显示=((窗口管理器)getSystemService(WINDOW_SERVICE))。
     getDefaultDisplay();
     INT或= cameraInfo.orientation;
     //你需要选择你的应用程序最合适的previewSize
    // ....选择previewSizes之一,在这里
     / * parameters.set previewSize(previewSize.width,previewSize.height); * /
    如果(display.getRotation()== Surface.ROTATION_0)
     {camera.setDisplayOrientation(90);
    或= 90;
    }如果(display.getRotation()== Surface.ROTATION_180)
    {
        camera.setDisplayOrientation(270);
    或= 270;
    }
    如果(display.getRotation()== Surface.ROTATION_270)
    {
        camera.setDisplayOrientation(180);
        或= 180;
    }parameters.setRotation(或); camera.setParameters(参数);
    尝试
    {
        camera.set previewDisplay(cameraSurfaceHolder);
        camera.start preVIEW();
        previewing = TRUE;
    }
    赶上(IOException异常E)
    {
        // TODO自动生成catch块
        e.printStackTrace();
    }
    }

    I was trying camera in android and thought of overrelay frame using surfaceview, found out this example.. Android creating camera and overrelay frame. Example was great.

    1.Started enhancing so changed the potrait and landscape frames, the camera view was not showing when i change to potrait so checked and got ans from this link stackoverflow(changing display orientation), Inserted code in surface changed for that..showed error in setparameters, so deleted, it worked

    2.But when i see the photo which i took it is showing like this..
    Image. Then again got ans from this link ExifInterface(orientatioin)

    1. Front Camera is not opening too.. ??

    cambutton.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageButton
    android:id="@+id/btnCapture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="500dp"
    android:background="@drawable/cam"
    android:layout_centerHorizontal="true"
    />
    <ImageButton
        android:id="@+id/btnswitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/switchcamera"
        android:layout_marginLeft="85dp"
        android:layout_marginStart="85dp"
        android:layout_marginTop="500dp"
        android:layout_toRightOf="@id/btnCapture"
        android:layout_toEndOf="@id/btnCapture"
        />
    
    </RelativeLayout>
    

    campotrait.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
    <SurfaceView
        android:id="@+id/cameraSurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/frame3"
        />
    </LinearLayout>
    

    camoverrelay1.xml is for landscape, the same code above

    Share.java

    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.content.res.Configuration;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.PixelFormat;
    import android.graphics.drawable.Drawable;
    import android.hardware.Camera;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.Display;
    import android.view.LayoutInflater;
    import android.view.Surface;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ImageButton;
    import android.widget.RelativeLayout;
    import android.widget.Toast;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.List;
    
    
    public class Share extends Activity implements SurfaceHolder.Callback{
    
    private Camera camera = null;
    private SurfaceView cameraSurfaceView = null;
    private SurfaceHolder cameraSurfaceHolder = null;
    private boolean previewing = false;
    
    private Display display = null;
    
    private static int wid = 0, hgt = 0;
    
    private LayoutInflater layoutInflater = null;
    private View cameraViewControl = null;
    private RelativeLayout.LayoutParams layoutParamsControl = null;
    
    private ImageButton btnCapture = null;
    private ImageButton btnswitch = null;
    Camera.Size previewSize=null;
    Camera.Size optimalSize = null;
    Bitmap mBitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        display = getWindowManager().getDefaultDisplay();
        wid = display.getWidth();
        hgt = display.getHeight();
    
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
     getWindow().setFlags(WindowManager.LayoutParams.
      FLAG_FULLSCREEN,   WindowManager.
      LayoutParams.FLAG_FULLSCREEN);
        int orientation = getResources().getConfiguration().orientation;
      /*  Camera.Parameters p = camera.getParameters();
        p.set("orientation", "portrait");
        p.set("rotation", 90);
        camera.setParameters(p);*/
        if ((orientation == Configuration.ORIENTATION_PORTRAIT)) {
         /*   camera.setDisplayOrientation(90);*/
            setContentView(R.layout.campotrait);
        }
        else{
           /* camera.setDisplayOrientation(180);*/
            setContentView(R.layout.cameraoverlay1);
        }
    
        cameraSurfaceView = (SurfaceView)findViewById(R.id.cameraSurfaceView);
        cameraSurfaceHolder = cameraSurfaceView.getHolder();
        cameraSurfaceHolder.addCallback(this);
        cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
        layoutInflater = LayoutInflater.from(getBaseContext());
        layoutParamsControl = new RelativeLayout.LayoutParams
     (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.
     FILL_PARENT);
    
        cameraViewControl = layoutInflater.inflate(R.layout.cambutton, null);
        this.addContentView(cameraViewControl, layoutParamsControl);
        btnCapture = (ImageButton)findViewById(R.id.btnCapture);
        btnswitch = (ImageButton) findViewById(R.id.btnswitch);
        btnCapture.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                camera.takePicture(cameraShutterCallback,
                        cameraPictureCallbackRaw,
                        cameraPictureCallbackJpeg);
    
            }
        });
        btnswitch.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                int camerasNumber = Camera.getNumberOfCameras();
                if (camerasNumber > 1) {
                    //release the old camera instance
                    //switch camera, from the front and the back and vice versa
                    openFrontFacingCameraGingerbread();
                } else {
     Toast toast = Toast.makeText(getApplicationContext(),  "Sorry, your 
     phone has only one camera!", Toast.LENGTH_LONG);
                    toast.show();
                }
            }
        });
     }
     private Camera openFrontFacingCameraGingerbread() {
        int cameraCount = 0;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    cam = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    e.printStackTrace();
     Toast.makeText(getApplicationContext(),"Failed to Open 
      Front Camera",Toast.LENGTH_SHORT).show();
                }
            }
        }
    
        return cam;
     }
    Camera.ShutterCallback cameraShutterCallback = new Camera.ShutterCallback()
    {
        @Override
        public void onShutter()
        {
    
        }
     };
    
     Camera.PictureCallback cameraPictureCallbackRaw = 
     new Camera.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
    
        }
    };
    
    Camera.PictureCallback cameraPictureCallbackJpeg = 
    new Camera.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
            // TODO Auto-generated method stub
    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    
            wid =cameraBitmap.getWidth();
            hgt = cameraBitmap.getHeight();
    
    
    Bitmap newImage = Bitmap.createBitmap(wid,hgt, Bitmap.Config.ARGB_8888);
    
            Canvas canvas = new Canvas(newImage);
            canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
            int orientation = getResources().getConfiguration().orientation;
    
            if ((orientation == Configuration.ORIENTATION_PORTRAIT)){
                Drawable drawable = getResources().getDrawable
                        (R.drawable.potraitimage);
                assert drawable != null;
                drawable.setBounds(0, 0, wid,hgt);
                drawable.draw(canvas);
    
            }
            else{
                Drawable drawable = getResources().getDrawable
                        (R.drawable.curtain);
                assert drawable != null;
                drawable.setBounds(0, 0, wid,hgt);
                drawable.draw(canvas);
    
            }
         /*   Bitmap bitm = null;*/
            File storagePath = new File(Environment.
                    getExternalStorageDirectory() + "/ActEventz/");
            storagePath.mkdirs();
    
            File myImage = new File(storagePath, "test.jpg");
     ExifInterface exif;
            try {
                exif = new ExifInterface(storagePath + "test.jpg");
                int orient = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION, 0);
                Log.d("EXIF", "Exif: " + orient);
                Matrix matrix = new Matrix();
                if (orient == 0) {
                    matrix.postRotate(0);
                    Log.d("EXIF", "Exif: " + orient);
                } else if (orient == 90) {
                    matrix.postRotate(0);
                    Log.d("EXIF", "Exif: " + orient);
                }
                mBitmap= Bitmap.createBitmap(cameraBitmap, 0, 
       0,cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
             mBitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
            byte[] byteArray= stream.toByteArray();
            Intent i = new Intent(getApplicationContext(), Imageview.class);
            Bundle bu = new Bundle();
            bu.putByteArray("photo",byteArray );
            i.putExtras(bu);
            startActivity(i);
            try
            {
                FileOutputStream out = new FileOutputStream(myImage);
                newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
       Toast.makeText(getApplicationContext(), "Image Captured",
       Toast.LENGTH_SHORT).show();
                out.flush();
                out.close();
            } catch(IOException e)
            {
                Log.d("In Saving File", e + "");
            }
          /*  Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap bitmap = BitmapFactory.decodeFile(storagePath+"test.jpg");
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotatedBitmap, 
     hgt,wid,true);
    
            scaledBitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);*/
    
            camera.startPreview();  
            newImage.recycle();
            newImage = null;
            cameraBitmap.recycle();
            cameraBitmap = null;
        }
    };
    @Override
    public void onRequestPermissionsResult(int requestCode, 
    String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 200: {
                // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0 && grantResults[0] ==PackageManager.
     PERMISSION_GRANTED) {
                    //Permission Granted
                } else {
                    //Permission Denied
                }
                return;
      //Use other case lines for other requests (with different requestCodes)
            }
        }
     }
     @Override
     public void surfaceChanged(SurfaceHolder holder,
                               int format, int width, int height)
     {
        // TODO Auto-generated method stub
    
        if(previewing)
        {
            camera.stopPreview();
            previewing = false;
        }
    
            Camera.Parameters parameters = camera.getParameters();
     Display display = ((WindowManager)getSystemService
     (WINDOW_SERVICE)).getDefaultDisplay();
     List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
             previewSize=previewSizes.get(0);
    
         // You need to choose the most appropriate previewSize for your app
         // .... select one of previewSizes here
     /* parameters.setPreviewSize(previewSize.width, previewSize.height);*/
           if(display.getRotation() == Surface.ROTATION_0)
            {
                parameters.setPreviewSize(previewSize.height,previewSize.width);
                camera.setDisplayOrientation(90);
    
            }
    
            if(display.getRotation() == Surface.ROTATION_90)
            {
                parameters.setPreviewSize(previewSize.width,previewSize.height);
    
            }
    
            if(display.getRotation() == Surface.ROTATION_180)
            {
                parameters.setPreviewSize(previewSize.height,previewSize.width);
            }
    
            if(display.getRotation() == Surface.ROTATION_270)
            {
                parameters.setPreviewSize(previewSize.width,previewSize.height);
                camera.setDisplayOrientation(180);
    
            }
           /*camera.setParameters(parameters);*/
        //here error occurs setParameters failed 
        try
        {
                camera.setPreviewDisplay(cameraSurfaceHolder);
                camera.startPreview();
                previewing = true;
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
      }
    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        // TODO Auto-generated method stub
        try
        {
            camera = Camera.open();
        }
        catch(Exception e)
        {
     Toast.makeText(getApplicationContext(), "Device camera is not 
    working properly, please try after sometime.", Toast.LENGTH_LONG).show();
        }
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
    }
    

    I have searched many sites,stackoverflow, i can't get correct answer, The Only Error is the capture view of camera please suggest me solutions guys.. i am stuck on this the whole day.. Thank you in advance.

    解决方案

    Thanks for the Support Guys.. I have solved this problem by just changing some codes in Surface Changed.. I have deleted the ExifInterface section itself.. Just done below coding in surfacechanged the image saves as expected.. :)

    @Override
    public void surfaceChanged(SurfaceHolder holder,
                       int format, int width, int height)
    {
    // TODO Auto-generated method stub
    
    if(previewing)
    {
    camera.stopPreview();
    previewing = false;
    }
    Camera.Parameters parameters = camera.getParameters();
      Display display =   ((WindowManager)getSystemService(WINDOW_SERVICE)).
     getDefaultDisplay();
     int or=cameraInfo.orientation;
     // You need to choose the most appropriate previewSize for your app
    // .... select one of previewSizes here
     /* parameters.setPreviewSize(previewSize.width,    previewSize.height);*/
    if(display.getRotation() == Surface.ROTATION_0)
     {
    
    camera.setDisplayOrientation(90);
    or=90;
    }
    
    if(display.getRotation() == Surface.ROTATION_180)
    {
        camera.setDisplayOrientation(270);
    or=270;
    }
    if(display.getRotation() == Surface.ROTATION_270)
    {
        camera.setDisplayOrientation(180);
        or=180;
    }
    
    parameters.setRotation(or);
    
     camera.setParameters(parameters);
    try
    {
        camera.setPreviewDisplay(cameraSurfaceHolder);
        camera.startPreview();
        previewing = true;
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    

    这篇关于摄像头视图SurfaceView存储在风景只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆