Android blic void onPictureTaken(字节[]数据,摄像头){ 试一试{ mFileTemp=new SaveImageTask().execute(data.get(); }捕捉(中断异常e){ e、 printStackTrace(); }捕获(执行例外){ e、 printStackTrace(); } 复位凸轮(); Log.d(标签“onPictureTaken-jpeg”); } }; 私有类SaveImageTask扩展了AsyncTask{ @凌驾 受保护文件doInBackground(字节[]…数据){ FileOutputStream扩展流=null; 文件outFile=null; //写入SD卡 试一试{ 文件sdCard=Environment.getExternalStorageDirectory(); File dir=新文件(sdCard.getAbsolutePath()+“/camtest”); dir.mkdirs(); 字符串文件名=String.format(“%d.jpg”,System.currentTimeMillis()); outFile=新文件(dir,fileName); outStream=新文件OutputStream(输出文件); 超流写入(数据[0]); 冲水; exptream.close(); Log.d(标记“onPictureTaken-写入字节:“+data.length+”到“+outFile.getAbsolutePath()”); 刷新画廊(outFile); }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); }最后{ } 返回输出文件; } }

Android blic void onPictureTaken(字节[]数据,摄像头){ 试一试{ mFileTemp=new SaveImageTask().execute(data.get(); }捕捉(中断异常e){ e、 printStackTrace(); }捕获(执行例外){ e、 printStackTrace(); } 复位凸轮(); Log.d(标签“onPictureTaken-jpeg”); } }; 私有类SaveImageTask扩展了AsyncTask{ @凌驾 受保护文件doInBackground(字节[]…数据){ FileOutputStream扩展流=null; 文件outFile=null; //写入SD卡 试一试{ 文件sdCard=Environment.getExternalStorageDirectory(); File dir=新文件(sdCard.getAbsolutePath()+“/camtest”); dir.mkdirs(); 字符串文件名=String.format(“%d.jpg”,System.currentTimeMillis()); outFile=新文件(dir,fileName); outStream=新文件OutputStream(输出文件); 超流写入(数据[0]); 冲水; exptream.close(); Log.d(标记“onPictureTaken-写入字节:“+data.length+”到“+outFile.getAbsolutePath()”); 刷新画廊(outFile); }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); }最后{ } 返回输出文件; } },android,android-camera,custom-component,Android,Android Camera,Custom Component,} 预习班 class Preview extends ViewGroup implements SurfaceHolder.Callback { private final String TAG = "Preview"; SurfaceView mSurfaceView; SurfaceHolder mHolder; Size mPreviewSize; List<Size> mSupportedPreviewSizes; C

}

预习班

   class Preview extends ViewGroup implements SurfaceHolder.Callback {
    private final String TAG = "Preview";

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;
    Size mPreviewSize;
    List<Size> mSupportedPreviewSizes;
    Camera mCamera;
    Context mContext;

    Preview(Context context, SurfaceView sv) {
        super(context);
        mContext = context;
        mSurfaceView = sv;
//        addView(mSurfaceView);

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void setCamera(Camera camera) {
        mCamera = camera;
        if (mCamera != null) {
            mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
            requestLayout();

            // get Camera parameters
            Camera.Parameters params = mCamera.getParameters();

            List<String> focusModes = params.getSupportedFocusModes();
            if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
                // set the focus mode
                params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                // set Camera parameters
                mCamera.setParameters(params);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // We purposely disregard child measurements because act as a
        // wrapper to a SurfaceView that centers the camera preview instead
        // of stretching it.
        final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);

        if (mSupportedPreviewSizes != null) {
            mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed && getChildCount() > 0) {
            final View child = getChildAt(0);

            final int width = r - l;
            final int height = b - t;

            int previewWidth = width;
            int previewHeight = height;
            if (mPreviewSize != null) {
                previewWidth = mPreviewSize.width;
                previewHeight = mPreviewSize.height;
            }

            // Center the child SurfaceView within the parent.
            if (width * previewHeight > height * previewWidth) {
                final int scaledChildWidth = previewWidth * height / previewHeight;
                child.layout((width - scaledChildWidth) / 2, 0,
                        (width + scaledChildWidth) / 2, height);
            } else {
                final int scaledChildHeight = previewHeight * width / previewWidth;
                child.layout(0, (height - scaledChildHeight) / 2,
                        width, (height + scaledChildHeight) / 2);
            }
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        try {
            if (mCamera != null) {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
        } catch (IOException exception) {
            Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        if (mCamera != null) {
            mCamera.stopPreview();
        }
    }


    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if(mCamera != null) {
            Camera.Parameters parameters = mCamera.getParameters(); 
//          parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
            Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

                if(display.getRotation() == Surface.ROTATION_0)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);                           
                    mCamera.setDisplayOrientation(90);
                }

                if(display.getRotation() == Surface.ROTATION_90)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);                           
                }

                if(display.getRotation() == Surface.ROTATION_180)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);               
                }

                if(display.getRotation() == Surface.ROTATION_270)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                    mCamera.setDisplayOrientation(180);
                }

                requestLayout();


            mCamera.setParameters(parameters);
            mCamera.startPreview();
        }
    }

}
类预览扩展视图组实现SurfaceHolder.Callback{
私有最终字符串标记=“预览”;
SurfaceView mSurfaceView;
表面粗糙度;
大小mPreviewSize;
列出MSSupportedPreviewsizes;
麦克默拉照相机;
语境;
预览(上下文,SurfaceView sv){
超级(上下文);
mContext=上下文;
mSurfaceView=sv;
//addView(mSurfaceView);
mHolder=mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE\u TYPE\u PUSH\u缓冲区);
}
公共摄像机(摄像机){
mCamera=摄像机;
if(mCamera!=null){
mSupportedPreviewSizes=mCamera.getParameters().getSupportedPreviewSizes();
requestLayout();
//获取相机参数
Camera.Parameters params=mCamera.getParameters();
List focusModes=params.getSupportedFocusModes();
if(focusModes.contains(Camera.Parameters.FOCUS\u MODE\u AUTO)){
//设置焦点模式
params.setFocusMode(摄像头.Parameters.FOCUS_MODE_AUTO);
//设置相机参数
mCamera.setParameters(参数);
}
}
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
//我们故意忽略儿童的测量,因为这是一种
//包装到将摄影机预览居中的SurfaceView
//我想把它拉长。
最终整数宽度=resolveSize(getSuggestedMinimumWidth(),widthMeasureSpec);
最终整数高度=resolveSize(getSuggestedMinimumHeight(),heightMeasureSpec);
设置测量尺寸(宽度、高度);
if(mSupportedPreviewSizes!=null){
mPreviewSize=getOptimalPreviewSize(MSSupportedPreviewSize、宽度、高度);
}
}
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
如果(已更改&&getChildCount()>0){
最终视图子对象=getChildAt(0);
最终整数宽度=r-l;
最终内部高度=b-t;
int previewWidth=宽度;
亮度=高度;
if(mPreviewSize!=null){
previewWidth=mPreviewSize.width;
PreviewView=mPreviewSize.height;
}
//将子曲面视图居中放置在父曲面视图中。
如果(宽度*预览宽度>高度*预览宽度){
最终整数缩放儿童宽度=预览宽度*高度/预览宽度;
布局((宽度-scaledChildWidth)/2,0,
(宽度+缩放儿童宽度)/2,高度);
}否则{
最终int scaledChildHeight=预览视图*宽度/预览宽度;
布局(0,(高度-缩放儿童高度)/2,
宽度(高度+缩放儿童高度)/2);
}
}
}
已创建的公共空白表面(表面持有人){
//曲面已创建,获取摄影机并告知其位置
//画。
试一试{
if(mCamera!=null){
mCamera.setPreviewDisplay(支架);
mCamera.startPreview();
}
}捕获(IOException异常){
Log.e(标记“setPreviewDisplay()引起的IOException”,exception);
}
}
公共空间表面覆盖(表面覆盖物持有人){
//当我们返回时,曲面将被破坏,因此请停止预览。
if(mCamera!=null){
mCamera.stopPreview();
}
}
私有大小GetOptimizePreviewSize(列表大小,整数w,整数h){
最终双纵横比公差=0.1;
双目标率=(双)w/h;
如果(size==null)返回null;
Size=null;
double minDiff=double.MAX_值;
int targetHeight=h;
//尝试找到与纵横比和大小匹配的大小
用于(尺寸:尺寸){
双倍比率=(双倍)size.width/size.height;
如果(数学abs(比率-目标比率)>纵横比公差)继续;
if(数学绝对值(尺寸高度-目标光)   class Preview extends ViewGroup implements SurfaceHolder.Callback {
    private final String TAG = "Preview";

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;
    Size mPreviewSize;
    List<Size> mSupportedPreviewSizes;
    Camera mCamera;
    Context mContext;

    Preview(Context context, SurfaceView sv) {
        super(context);
        mContext = context;
        mSurfaceView = sv;
//        addView(mSurfaceView);

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void setCamera(Camera camera) {
        mCamera = camera;
        if (mCamera != null) {
            mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
            requestLayout();

            // get Camera parameters
            Camera.Parameters params = mCamera.getParameters();

            List<String> focusModes = params.getSupportedFocusModes();
            if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
                // set the focus mode
                params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                // set Camera parameters
                mCamera.setParameters(params);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // We purposely disregard child measurements because act as a
        // wrapper to a SurfaceView that centers the camera preview instead
        // of stretching it.
        final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);

        if (mSupportedPreviewSizes != null) {
            mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed && getChildCount() > 0) {
            final View child = getChildAt(0);

            final int width = r - l;
            final int height = b - t;

            int previewWidth = width;
            int previewHeight = height;
            if (mPreviewSize != null) {
                previewWidth = mPreviewSize.width;
                previewHeight = mPreviewSize.height;
            }

            // Center the child SurfaceView within the parent.
            if (width * previewHeight > height * previewWidth) {
                final int scaledChildWidth = previewWidth * height / previewHeight;
                child.layout((width - scaledChildWidth) / 2, 0,
                        (width + scaledChildWidth) / 2, height);
            } else {
                final int scaledChildHeight = previewHeight * width / previewWidth;
                child.layout(0, (height - scaledChildHeight) / 2,
                        width, (height + scaledChildHeight) / 2);
            }
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        try {
            if (mCamera != null) {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
        } catch (IOException exception) {
            Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        if (mCamera != null) {
            mCamera.stopPreview();
        }
    }


    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if(mCamera != null) {
            Camera.Parameters parameters = mCamera.getParameters(); 
//          parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
            Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

                if(display.getRotation() == Surface.ROTATION_0)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);                           
                    mCamera.setDisplayOrientation(90);
                }

                if(display.getRotation() == Surface.ROTATION_90)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);                           
                }

                if(display.getRotation() == Surface.ROTATION_180)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);               
                }

                if(display.getRotation() == Surface.ROTATION_270)
                {
                    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                    mCamera.setDisplayOrientation(180);
                }

                requestLayout();


            mCamera.setParameters(parameters);
            mCamera.startPreview();
        }
    }

}
String path = *your_image_path*;
Bitmap scaledBitmap = null;
ExifInterface ei = new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
    scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),ScalingLogic.FIT, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
    scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),ScalingLogic.FIT, 180);
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
int cameraRotation = info.orientation;